带有 UIFontWeightTraits 的 NSAttributedString
NSAttributedString with UIFontWeightTraits
我正在尝试生成一个 NSAttributedString
,它基本上只有默认字体、斜体和不同的文本颜色。很简单,到目前为止。
现在我希望整个字符串的不同子字符串 另外 是粗体。它基本上应该是这样的:
来自 John Smith,25 月 08 日。在 8:00
(只是颜色不同。)
看来我把字典弄错了,我将其传递给 NSMutableAttributedString
的 addAttributes(_:_:)
函数。从documentation,我了解到这本字典应该是这样的:
[UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]
但这似乎不起作用。我最终得到的只是字符串的斜体版本。我显然在这里弄错了。有什么想法吗?
更新:添加简单示例
// Preparation
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
let italicFont = UIFont.italicSystemFontOfSize(14)
// Make entire string italic: works!
attributedString.addAttributes([NSFontAttributeName : italicFont], range: NSMakeRange(0, 33))
// Make the name string additionally bold: doesn't work!
attributedString.addAttributes([UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]], range: nameRange)
// Show it on the label
attributedStringLabel.attributedText = attributedString
谢谢!
UIFontDescriptorTraitsAttribute
不是 NSAttributedString
属性字典中可识别的键,因此在获得特征后,您需要重建 UIFont
并使用 NSFontAttributeName
键。
//prepare the fonts. we derive the bold-italic font from the italic font
let italicFont = UIFont.italicSystemFontOfSize(14)
let italicDesc = italicFont.fontDescriptor()
let italicTraits = italicDesc.symbolicTraits.rawValue
let boldTrait = UIFontDescriptorSymbolicTraits.TraitBold.rawValue
let boldItalicTraits = UIFontDescriptorSymbolicTraits(rawValue:italicTraits | boldTrait)
let boldItalicDescriptor = italicDesc.fontDescriptorWithSymbolicTraits(boldItalicTraits)
let boldItalicFont = UIFont(descriptor: boldItalicDescriptor, size: 0.0)
//prepare the string
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let fullRange = NSMakeRange(0, 33)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
attributedString.addAttributes([NSFontAttributeName:italicFont], range: fullRange)
attributedString.addAttributes([NSFontAttributeName:boldItalicFont], range: nameRange)
另请参阅:
我正在尝试生成一个 NSAttributedString
,它基本上只有默认字体、斜体和不同的文本颜色。很简单,到目前为止。
现在我希望整个字符串的不同子字符串 另外 是粗体。它基本上应该是这样的:
来自 John Smith,25 月 08 日。在 8:00
(只是颜色不同。)
看来我把字典弄错了,我将其传递给 NSMutableAttributedString
的 addAttributes(_:_:)
函数。从documentation,我了解到这本字典应该是这样的:
[UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]
但这似乎不起作用。我最终得到的只是字符串的斜体版本。我显然在这里弄错了。有什么想法吗?
更新:添加简单示例
// Preparation
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
let italicFont = UIFont.italicSystemFontOfSize(14)
// Make entire string italic: works!
attributedString.addAttributes([NSFontAttributeName : italicFont], range: NSMakeRange(0, 33))
// Make the name string additionally bold: doesn't work!
attributedString.addAttributes([UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]], range: nameRange)
// Show it on the label
attributedStringLabel.attributedText = attributedString
谢谢!
UIFontDescriptorTraitsAttribute
不是 NSAttributedString
属性字典中可识别的键,因此在获得特征后,您需要重建 UIFont
并使用 NSFontAttributeName
键。
//prepare the fonts. we derive the bold-italic font from the italic font
let italicFont = UIFont.italicSystemFontOfSize(14)
let italicDesc = italicFont.fontDescriptor()
let italicTraits = italicDesc.symbolicTraits.rawValue
let boldTrait = UIFontDescriptorSymbolicTraits.TraitBold.rawValue
let boldItalicTraits = UIFontDescriptorSymbolicTraits(rawValue:italicTraits | boldTrait)
let boldItalicDescriptor = italicDesc.fontDescriptorWithSymbolicTraits(boldItalicTraits)
let boldItalicFont = UIFont(descriptor: boldItalicDescriptor, size: 0.0)
//prepare the string
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let fullRange = NSMakeRange(0, 33)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
attributedString.addAttributes([NSFontAttributeName:italicFont], range: fullRange)
attributedString.addAttributes([NSFontAttributeName:boldItalicFont], range: nameRange)
另请参阅: