How can I make an NSTextField that has focus redraw itself?

I've subclassed and defined my own custom NSFormatter and NSTextField classes in order properly display SMPTE time code values ( HH:MM:SS:FF ). When the user selects to use drop-frame for the fps value, the format changes to HH:MM:SS;FF (semi-colon at the end). I have this all working well except for one final hurdle.

When changing the format, the NSTextField with the focus does not update it's display until it's focus is lost. Here is a screenshot to illustrate:

更改为丢帧格式

I have tried sending the NSTextField a setNeedsDisplay message when the format is changed, as well as sending it a display message. Nothing seems to work so far, and the text field with focus refuses to update/redraw itself until it loses focus.

Any suggestions of what I need to do to make this happen?


I did manage to solve this problem now. It may be a bit of a hack, but basically what I ended up doing was retrieving the NSTextField 's field editor with currentEditor and sending it as an object with the posted notification for when the format changes.

With the field editor I then just had it replaceCharactersInRange:withString: to change the last ":" to ";" and vice-versa. Though it may not be optimal, it works perfectly.

In fact, I've posted the solution to github for anyone else who might find this useful. https://github.com/cfloisand/smpteformatter

链接地址: http://www.djcxy.com/p/85246.html

上一篇: NSTextField和FirstResponder

下一篇: 我怎样才能使一个NSTextField焦点重绘本身?