简体   繁体   English

如何设置在 NSTextStorage 中输入的 *next* 字符的属性

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

It's simple enough to change the text attributes in a given range of NSTextStorage .NSTextStorage的给定范围内更改文本属性非常NSTextStorage

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

But how do I change the attributes at the current cursor position so that the next character has different attributes?但是如何更改当前光标位置的属性,以便下一个字符具有不同的属性?

(Think someone clicking the "bold" button in a toolbar with no text selected. No text turns bold, but the next character is bold.) (想想有人在没有选择文本的情况下单击工具栏中的“粗体”按钮。没有文本变成粗体,但下一个字符是粗体。)

You can use UITextView.typingAttributes to achieve this.您可以使用UITextView.typingAttributes来实现这一点。

This dictionary contains the attribute keys (and corresponding values) to apply to newly typed text.该字典包含要应用于新键入的文本的属性键(和相应的值)。 When the text view's selection changes, the contents of the dictionary are cleared automatically.当文本视图的选择发生变化时,字典的内容会自动清除。

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