How can I replace text in UITextView with NSUndoManager support?

前端 未结 4 556
清歌不尽
清歌不尽 2021-02-06 01:00

I want to be able to replace some text in an UITextView programatically, so I wrote this method as an UITextView category:

- (void) replaceCharactersInRange:(NSR         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-06 01:05

    There is actually no reason for any hacks or custom categories to accomplish this. You can use the built in UITextInput Protocol method replaceRange:withText:. For inserting text you can simply do:

    [textView replaceRange:textView.selectedTextRange withText:replacementText];
    

    This works as of iOS 5.0. Undo works automatically and there are no weird scrolling issues.

提交回复
热议问题