簡體   English   中英

如何設置在 NSTextStorage 中輸入的 *next* 字符的屬性

[英]How to set attributes of the *next* character that is typed in NSTextStorage

NSTextStorage的給定范圍內更改文本屬性非常NSTextStorage

storage.addAttribute(.font, value: UIFont.systemFont(ofSize: 30), at: range)

但是如何更改當前光標位置的屬性,以便下一個字符具有不同的屬性?

(想想有人在沒有選擇文本的情況下單擊工具欄中的“粗體”按鈕。沒有文本變成粗體,但下一個字符是粗體。)

您可以使用UITextView.typingAttributes來實現這一點。

該字典包含要應用於新鍵入的文本的屬性鍵(和相應的值)。 當文本視圖的選擇發生變化時,字典的內容會自動清除。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    // This is the key part
    textView.typingAttributes = myCurrentAttributes

    // Allow system to control typing
    return true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM