【发布时间】:2019-12-02 23:36:30
【问题描述】:
我正在尝试使用 UITextView 构建 RichTextEditor。 当我在文本末尾应用不同的字体大小时,它无法为输入的新文本更新字体大小。
我目前有以下代码,我试图让它工作:
富文本出口是 @IBOutlet weak var richTextView: UITextView!
尝试更改字体的代码是
func setAttributesForSelectedTextRange(_ attributes: [NSAttributedString.Key: Any]) {
if let text = richTextView.attributedText.mutableCopy() as? NSMutableAttributedString, let selectedRange = richTextView.selectedTextRange {
let cursorPosition = richTextView.offset(from: richTextView.beginningOfDocument, to: selectedRange.start)
text.addAttributes(attributes, range: richTextView.selectedRange)
if selectedRange.end == richTextView.endOfDocument {
text.append(NSAttributedString(string: "", attributes: attributes))
}
richTextView.attributedText = text
}
}
我尝试在末尾添加一个带有新属性的空字符串,但没有成功。 上例中如何实现设置属性变化?
【问题讨论】:
标签: swift uitextview nsattributedstring rich-text-editor nsmutableattributedstring