我想在UITextView中混合2种字体。Font1 具有特殊的 unicode 字符,font2 没有。如果 UITextView 的主字体是 font2,如何在不让光标跳到末尾的情况下将 Font1 插入 UITextView?由于 .attributedText 是不可变的。我假设我需要创建一个 NSMutableString 并将其分配给 .attributedText。但是,这会将光标放在末尾。有没有办法在不让光标跳到末尾的情况下在光标选择处插入字体 1?提前感谢任何帮助。
以下函数在光标点中插入属性。
https://developer.apple.com/documentation/foundation/nsmutableattributedstring/1414947-insert
func insertAtTextViewCursor(attributedString: NSAttributedString) {
// Find out the cursor point
guard let selectedRange = textView.selectedTextRange else { return }
// If cursor point found
let cursorIndex = textView.offset(from: textView.beginningOfDocument, to: selectedRange.start)
let mutableAttributedText = NSMutableAttributedString(attributedString: textView.attributedText)
mutableAttributedText.insert(attributedString, at: cursorIndex)
textView.attributedText = mutableAttributedText
}