问题
Whenever I disable an NSTextField the text loses its color and changes to gray. How can I disable the text field and keep the text color? In my particular situation some words are colored red. I'd like those words to stay red when I disable.
回答1:
You need to subclass NSTextField
to do that. See this CocoaBuilder thread:
- (void)setEnabled:(BOOL)flag
{
[super setEnabled:flag];
if (!flag) {
[self setTextColor:[NSColor secondarySelectedControlColor]];
} else {
[self setTextColor:[NSColor controlTextColor]];
}
}
回答2:
I discovered that while I was editing the text I could set colors and everything, but the problem of the text losing color was happening even when I didn't disable the text box. I had neglected to allow my NSTextField to allow rich text. Now the textfield keeps all colors even when I setEditable:NO
.
来源:https://stackoverflow.com/questions/23430007/disable-nstextfield-without-changing-color-of-multi-colored-text