我有一个方法将NSAttributedString设置为粗体:
func setBold(text: String) -> NSMutableAttributedString {
guard let font = UIFont.CustomNormalBoldItalic() else {
fatalError("font not found")
}
let string = NSMutableAttributedString(string:"\(text)", attributes: [NSFontAttributeName : font])
self.setAttributedString(string)
return self
}
Run Code Online (Sandbox Code Playgroud)
这就是它的调用方式,它正常工作:
let formattedString = NSMutableAttributedString()
formattedString.setBold("Your text here")
Run Code Online (Sandbox Code Playgroud)
但是,我试图将NSLocalizedString的子字符串的文本设置为粗体.所以我会这样尝试:
let formattedString = NSMutableAttributedString()
return NSAttributedString(string: String.localizedStringWithFormat(
NSLocalizedString("message", comment: ""),
formattedString.setBold(NSLocalizedString("message.day", comment: "")),
NSLocalizedString("message.time", comment: "")
))
Run Code Online (Sandbox Code Playgroud)
它不是" 今天晚上10点开始",而是提供以下输出:
Today{
NSFont = "<UICTFont: 0x7fb75d4f1330> font-family: \"CustomText-MediumItalic\"; font-weight: normal; font-style: italic; font-size: 14.00pt";
} starting at 10pm {
} …Run Code Online (Sandbox Code Playgroud)