How to bind a NSTextField to a raw string while displaying another attributed string?

怎甘沉沦 提交于 2020-01-24 20:14:29

问题


I'm using view-based NSTableView and would like to allow users to edit text in place. Cocoa typically has superb support for this, but I am not sure how to bind the value of NSTextField to one string while displaying a slightly different string to the user, as shown below. For example, when user clicks on a text field or presses enter key to edit, the field editor should only be filled with exportTest.zip and not exportTest.zip Shared, and whatever edit user makes should only affect the exportTest.zip value as well. Can this be done using cocoa text?


回答1:


This can easily be done with NSFormatter however just in case anyone else struggles as I did I thought I'd point out that in order to display a different string when editing you will need to override editingStringForObjectValue:




回答2:


Did you try to use an NSFormatter? You could formulate the string Shared similar as you would do with a currency symbol. Your model simple stores the file name and Shared is part of the format.



来源:https://stackoverflow.com/questions/9426255/how-to-bind-a-nstextfield-to-a-raw-string-while-displaying-another-attributed-st

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