我手里有这个代码:
if let text = trimText?.mutableCopy() as? NSMutableString {
text.insertString("\(prefix) ", atIndex: 0)
textStorage.replaceCharactersInRange(range, withString: text as String)
}
当我尝试将我的文本
更改为:
text = attributedTextFunc(text)
哪里
func attributedTextFunc(str: NSString) -> NSAttributedString {
var attributedString = NSMutableAttributedString(string: str as String, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(15.0)])
let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFontOfSize(15.0)]
attributedString.addAttributes(boldFontAttribute, range: str.rangeOfString("More"))
return attributedString
}
我收到此错误:
Cannot assign value of type 'NSAttributedString' to type 'NSMutableString'
怎样才能把它变成粗体?
最佳答案
您不能将 NSAttributedString 分配给文本。这是两种不同的类型。
String 不是 NSAttributedString 的子类。
您应该设置:
attributedText = attributedTextFunc(text)
然后如果你想把它呈现在UILabel上
label.attributedText = attributedText
更新
Struct String 对 UIKit 和 Bold 样式一无所知。
NSAttributedString 了解 UIKit 并包含您想要的任何文本样式
更新2
就你的情况
ReadMoreTextView.attributedTrimText = attributedText
关于ios - 如何使粗体成为 NSMutableString 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121004/