#FF00AA

Je m’appelle Cédric Bozzi, je crée des sites et des applications, et ceci est mon blog dédié à la technologie : vous y trouverez des news, des opinions et des tests, le tout écrit par un Mac-head aux opinions tranchées.

There’s an English version here, mais la majeure partie du contenu est tirée de mon flux Twitter, et donc disponible en une seule langue (qui change au hasard des humeurs).

28 fév. 2009

Changing the selected text’s color for an NSTextField

Yeah, I’m getting intimate with Cocoa development, so you can expect occasional posts like this one. My readers are not supposed to care about this; I’m just posting it because I’ve been googling that question for an hour and didn’t find a proper answer anywhere.

 

There’s a point in the Cocoa documentation (I would link to it, but it looks like URLs tend to change every so often) that says:

A text field allows you to set the attributes of its text, the text background color, whether it draws the background, and whether it draws a bezel or border around its text. Note that the text and background colors of selected text are configurable. The selected text color overrides any actual text color applied to the text while it’s selected (this is generally the case with controls).

But nowhere does it say how. NSTextView has a setSelectedTextAttributes, but NSTextField doesn’t; so how do you change the selected text’s attributes?

Maybe you’re supposed to know that from the start (I’m learning Cocoa in random order, by googling for bits of information as I need them, and I know that’s not how you’re supposed to do it but, hey, my app works, and it doesn’t even seem to leak), but NSTextView isn’t just a control you can drop in Interface Builder to let users enter long, formatted text; there’s also an NSTextView associated with each window, and it processes the text behind the scenes or something.

So all you have to do is get access to that NSTextView, and setSelectedTextAttributes on it. Here’s how you get text fields of the window InputWindow to show selected text on a black background:

NSTextView *textEditor = (NSTextView *)[InputWindow fieldEditor:YES forObject:InputTextField];

[textEditor setSelectedTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSColor blackColor], NSBackgroundColorAttributeName, nil]];

The (NSTextView*) bit is because fieldEditor:forObject: thinks it returns an NSText*, even though it seems like it does return an NSTextView* — otherwise the code just wouldn’t work, would it? Looks like a known bug in the Cocoa headers.

 

Archives

2001   01   02   03   04   05   06   07   08   09   10   11   12  

2002   01   02   03   04   05   06   07   08   09   10   11   12  

2003   01   02   03   04   05   06   07   08   09   10   11   12  

2004   01   02   03   04   05   06   07   08   09   10   11   12  

2005   01   02   03   04   05   06   07   08   09   10   11   12  

2006   01   02   03   04   05   06   07   08   09   10   11   12  

2007   01   02   03   04   05   06   07   08   09   10   11   12  

2008   01   02   03   04   05   06   07   08   09   10   11   12  

2009   01   02   03   04   05   06   07   08   09   10   11   12  

2010   01   02   03   04   05   06   07   08   09   10   11   12  

2011   01   02   03   04   05   06   07   08   09   10   11   12  

2012   01   02   03   04   05   06   07   08   09   10   11   12