在UITextView.attributedText插入Unicode的正确方法

问题描述 投票:0回答:1

我想在一个UITextView混合2种字体。 Font1具有特殊的Unicode字符,font2没有。如果UITextView中的主要字体font2,我怎么可以插入到Font1没有的UITextView光标跳到结束了吗?由于.attributedText是非可变的。我想我需要创建的NSMutableString并将其分配给.attributedText。然而,这将光标在最后。是否有插入字体1在光标选择,而不必光标跳到结束的方法吗?在此先感谢您的帮助。

ios uitextview nsattributedstring
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.