如何创建一个重点重绘的NSTextField?

时间:2021-09-26 07:16:46

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.

我已经子类化并定义了我自己的自定义NSFormatter和NSTextField类,以便正确显示SMPTE时间码值(HH:MM:SS:FF)。当用户选择使用drop-frame作为fps值时,格式将更改为HH:MM:SS; FF(结尾处的分号)。除了一个最后的障碍,我一切都运作良好。

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:

更改格式时,具有焦点的NSTextField在焦点丢失之前不会更新它的显示。这是一个截图来说明:

如何创建一个重点重绘的NSTextField?

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.

我曾尝试在格式更改时向NSTextField发送setNeedsDisplay消息,并向其发送显示消息。到目前为止似乎没有任何工作,并且具有焦点的文本字段拒绝更新/重绘自身,直到它失去焦点。

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

有什么建议我需要做些什么来实现这一目标?

1 个解决方案

#1


1  

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.

我现在设法解决了这个问题。它可能有点像黑客,但基本上我最终做的是使用currentEditor检索NSTextField的字段编辑器并将其作为对象发送,并在格式更改时发布通知。

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.

使用字段编辑器,我只需要将replaceCharactersInRange:withString:将最后一个“:”更改为“;”反之亦然。虽然它可能不是最佳的,但它完美地运作。

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

事实上,我已经将解决​​方案发布到github,以供其他可能认为有用的人使用。 https://github.com/cfloisand/smpteformatter

#1


1  

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.

我现在设法解决了这个问题。它可能有点像黑客,但基本上我最终做的是使用currentEditor检索NSTextField的字段编辑器并将其作为对象发送,并在格式更改时发布通知。

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.

使用字段编辑器,我只需要将replaceCharactersInRange:withString:将最后一个“:”更改为“;”反之亦然。虽然它可能不是最佳的,但它完美地运作。

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

事实上,我已经将解决​​方案发布到github,以供其他可能认为有用的人使用。 https://github.com/cfloisand/smpteformatter