Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField text and background colour with/without focus

I just ran into an issue with resetting colours when an NSTextField was selected.
The methods I'm using are

- (void) setBackgroundColor:(NSColor *)aColor 
- (void) setTextColor:(NSColor *)aColor

I found that when I clicked in the NSTextField and thereby placed the focus in that text field, then tried to change the colour, the colours were not reset until focus was placed outside of that text field.
The colour change otherwise worked when focus was not on the NSTextField to begin with.

like image 366
mcallister Avatar asked Oct 22 '22 10:10

mcallister


1 Answers

The simple solution I found without having to access the First Responder was to reset the box as editable/not editable after changing the colours.

- (void) setEditable:(BOOL)flag

By setting the value of flag to NO and then YES in 2 subsequent calls (or YES and then NO depending on the usage), this seems to remove the focus from the NSTextField and then place it back, also updating the colours.

Hope this helps.

like image 190
mcallister Avatar answered Oct 24 '22 09:10

mcallister