【问题标题】:How do you pass a specific font name with font size when making a string bold, italic, underlined, or strikethrough?在将字符串设为粗体、斜体、下划线或删除线时,如何传递具有字体大小的特定字体名称?
【发布时间】:2022-01-17 12:02:35
【问题描述】:

我编写了一些代码来使字符串变为粗体、斜体、下划线和删除线。

但在执行这些功能时,我想保持特定字体名称和大小不变,而不是将其默认为字体名称和字体大小的系统值。我如何做到这一点?

以下是为粗体、斜体、下划线和删除线添加的代码:

if textBold == true {
    let string = text
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.boldSystemFont(ofSize: 25)
    ]
    let attributedString = NSAttributedString(string: string, attributes: attributes)
    modifiedString = attributedString
    text_View.attributedText = modifiedString
    text_View.sizeToFit()
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textItalic == true {
    text_View.sizeToFit()
    let string = text
    
    let attributes: [NSAttributedString.Key: Any] = [
        .font: UIFont.italicSystemFont(ofSize: CGFloat(fontSize))
    ]
    let attributedString = NSAttributedString(string: string, attributes: attributes)
    self.modifiedString = attributedString
    text_View.attributedText = modifiedString
    
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textUnderline == true {
    text_View.sizeToFit()
    let string = text
    let attributedString = NSMutableAttributedString.init(string: string)
    attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:
                                    NSRange.init(location: 0, length: attributedString.length))
    self.modifiedString = attributedString
    text_View.attributedText = modifiedString
    
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

if textStrikethrough == true {
    text_View.sizeToFit()
    let string = text
    let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: string)
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
    self.modifiedString = attributeString
    text_View.attributedText = modifiedString
    databaseHandlerObj.editLabelData(textProjectId: fetchTextProjectId, text_Id: fetchTextId, text: modifiedString.string)
}

【问题讨论】:

  • 你的问题不合逻辑。你怎么知道你选择的字体系列包含粗体和斜体字体?此外,UIKit 没有像 Cocoa 那样的字体管理器。

标签: ios swift uilabel uitextview nsattributedstring


【解决方案1】:
let attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "name of your font", size: 12.0)]

或创建UIFont的扩展名

extension UIFont {
    class func customFont(size: CGFloat) -> UIFont {
        guard let font = UIFont(name: "font name", size: 12.0) else {
            return UIFont.systemFont(ofSize: 12.0)
        }
        return font
    }
}

像这样使用它:

let attributedString = NSAttributedString(string: string, attributes: [NSAttributedString.Key.font: UIFont.customFont(size: 12.0)])

【讨论】:

  • 我之前尝试过这段代码,但是代码中断了。
  • 试试这个修改后的答案。
  • 如何从问题中的“特定字体名称”转到systemFont(ofSize:)?您没有使用特定的字体系列,而是使用系统字体。
猜你喜欢
  • 2011-03-03
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多