Highlight a selection in NSTextField

好久不见. 提交于 2019-12-06 08:16:50
JWWalker

You may have noticed that an NSTextField only shows a selection range when it has focus, i.e., is the first responder. In this case, editing is handled by an NSTextView called the field editor. So, make sure the field has focus (e.g., by using the makeFirstResponder: method of NSWindow), then use the NSControl method currentEditor to get the field editor, and then you can use the NSText method setSelectedRange:.

ObjC

NSText* fieldEditor = [myField currentEditor];
[fieldEditor setSelectedRange: mySelRange];

Swift

let fieldEditor = textfield.currentEditor()
fieldEditor?.selectedRange = range
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!