【发布时间】:2019-02-22 13:35:18
【问题描述】:
我正在尝试在我的文本视图中添加带有属性文本的粗体字体。此字体将在某些按钮操作后应用,我的问题是每当我添加粗体字体时。它正在添加粗体并重复单词。在附图中,应在“hello”一词之后添加粗体字。所以它显示为 这是粗体
这是我尝试过的:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if boldFont {
let boldStyle = NSAttributedString(string: text, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20)])
textView.textStorage.replaceCharacters(in: range, with: boldStyle)
}
return true
}
【问题讨论】:
标签: swift textview nsattributedstring nsrange