Getting Text Colors Right for Derived NSTextFieldCell under Various Conditions
Getting Text Colors Right for Derived NSTextFieldCell under Various Conditions
- Subject: Getting Text Colors Right for Derived NSTextFieldCell under Various Conditions
- From: Grant Erickson <email@hidden>
- Date: Fri, 04 Dec 2009 10:52:49 -0800
- Thread-topic: Getting Text Colors Right for Derived NSTextFieldCell under Various Conditions
I have implemented a custom class that derives from NSTextFieldCell to draw
status for an associated device (not unlike the network source list in the
Network System Preferences) overriding only:
- (void) drawInteriorWithFrame: (NSRect)inFrame inView: (NSView *)inView;
drawing the interior that includes two images, three strings and one drawn
graphic.
The end result is something that looks like the following (in terms of
bounding boxes):
.------..------------------------. .------.
.--.| || |.---------------.| |
!__!| ||------------------------|!_______________!| |
!______!!________________________! !______!
Img Image Strings String Drawn
The issue I am having is determining the correct set of conditionals to use
to determine when to invert the color of my text strings from
backgroundColor (when the cell is selected and the associated table has
focus) to textColor (when the cell is not selected or is selected and does
not have focus). I currently use:
- (void) drawInteriorWithFrame: (NSRect)inFrame
inView: (NSView *)inView
{
const bool isHighlighted = [self isHighlighted];
const bool notOff = ([self state] != 0);
const NSResponder * const firstResponder =
[[[self controlView] window] firstResponder];
const bool isFirstResponder = (inView == firstResponder);
const bool invertText = ((isHighlighted || notOff) &&
isFirstResponder);
...
// Draw the title, status and value strings.
[self drawString: [theModel titleString]
withAttributes: [self titleAttributes: invertText]
withRect: titleRect
inView: inView];
which works in all cases except when I open a modal panel or sheet. I've
since modified the condition to add a check for
...
const bool controlWindowIsKey = [[[self controlView] window]
isKeyWindow];
...
const bool invertText = ((isHighlighted || notOff) &&
isFirstResponder &&
controlWindowIsKey);
which seems to work correctly; however, this collectively strikes me as
"heavier" than I suspect optimal for this text inversion test. Any
recommendations on a better test or set thereof or is this more or less
"state of the art" for such a thing?
Regards,
Grant
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden