NSTextView's insertText method is deprecated in OS X v10.11. What is the replacement?

ⅰ亾dé卋堺 提交于 2019-12-05 12:13:29

The documentation says

- (void)insertText:(id)aString

This method is the means by which text typed by the user enters an NSTextView. See the NSInputManager class and NSTextInput protocol specifications for more information. ...

In NSTextInput there is a note:

IMPORTANT

NSTextInput protocol is slated for deprecation. Please use NSTextInputClient protocol, introduced in OS X v10.5, as described in NSTextInputClient Protocol Reference.

In NSTextInputClient protocol there is a method

- (void)insertText:(id)aString
  replacementRange:(NSRange)replacementRange

This seems to be the appropriate replacement

Old method

- (void)insertText:(id)aString

New method

- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange

Using the new insertText method in practice

// Start String: abcdef

// Result: a++bcdef
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 0)];

// Result: a++def
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 2)];

// Result: a++f
[NSTextView insertText:@"++" replacementRange:NSMakeRange(1, 4)];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!