简体   繁体   English

NSMutableAttributedString上的临时属性在编辑字符串时被删除

[英]Temporary attributes on NSMutableAttributedString that are removed when string is edited

I was wondering if there is a way to set attributes on an NSMutableAttributedString that will go away when the string is edited. 我想知道是否有一种方法可以在NSMutableAttributedString上设置属性,该属性在编辑字符串时会消失。 For example, you know how if you type in some wrong text on your iOS device, it gets underlined with a red dotted line(Example: "dogg"). 例如,您知道如何在iOS设备上键入错误的文本时,该文本用红色虚线下划线(例如:“ dogg”)。 In this example, if you then would remove the last "g" so the word would now be "dog" the red dotted underline goes away since it's a valid English word. 在此示例中,如果您随后删除了最后一个“ g”,则该单词现在为“ dog”,则红色虚线下划线消失了,因为它是有效的英语单词。 How can I replicate this behavior with a custom UITextField? 如何使用自定义UITextField复制此行为? Currently I am using a NSMutableAttributedString to underline a portion of a custom string of mine but the problem is that the user can then go back, and add text to say the middle of the underlined part and the new text they type gets the underline also. 当前,我正在使用NSMutableAttributedString在我的自定义字符串的一部分下划线,但问题是用户可以返回,然后添加文本以表示带下划线的部分的中间部分,并且他们键入的新文本也将获得下划线。 I want a portion of my string to get an underline, like the red underline in the above example, but I want the underline to go away when the underlined portion of the string is edited. 我希望我的字符串的一部分带有下划线,就像上面示例中的红色下划线一样,但是当字符串的带下划线的部分被编辑时,我希望下划线消失。

How can I do this? 我怎样才能做到这一点?

Also, this is for a jailbreak tweak so I am totally open to using private api's. 另外,这是为了进行越狱调整,因此我完全可以使用私有api。

Thanks! 谢谢!

Two thoughts. 两个想法。

First, you generally don't need the string to be mutable -- you just have an attributed string, and the text field (or other controls) will take care of updating/replacing the string based on user input or other events. 首先,您通常不需要字符串是可变的-您只需拥有属性字符串,并且文本字段(或其他控件)将根据用户输入或其他事件来更新/替换字符串。

In the UITextFieldDelegate, you want generally want to use textField:shouldChangeCharactersInRange:replacementString: ... then, any time the user changes "dogg" to "dog" or whatever, you can reprocess your logic to determine what attributes to show. 在UITextFieldDelegate中,通常希望使用textField:shouldChangeCharactersInRange:replacementString: ...然后, 只要用户将“ dogg”更改为“ dog”或其他任何内容,就可以重新处理逻辑以确定要显示的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM