Re: Highlighting chars in an NSTextField?
Re: Highlighting chars in an NSTextField?
- Subject: Re: Highlighting chars in an NSTextField?
- From: Fritz Anderson <email@hidden>
- Date: Thu, 26 Jan 2012 16:19:02 -0600
On 26 Jan 2012, at 1:31 PM, Clark Williams wrote:
> I have a NSTextField with a string displayed. I want to hightlight the 1st 2 chars then the next 2, then the next 2, etc.
> I tried the following code:
> but does not seem to work. At least it doesn't highlight anything.
> [currentDataTextBox setStringValue:@"AaBbCcDdEeFfGgHh"];
> NSCell* currentCell = [currentDataTextBox cell];
> NSView *currentView4Cell = [currentCell controlView];
> NSRect currentRect = [currentDataTextBox bounds];
> // guess at the points/character
> double pointsPerChar = currentRect.size.width / [[currentDataTextBox stringValue] length];
> NSLog ( @"pt/char: %f", pointsPerChar );
> currentRect.origin.x += 0.0; // bump this to change the origin in loop
> currentRect.size.width = pointsPerChar * 2.0;
> [currentCell highlight:YES withFrame:currentRect inView:currentView4Cell];
>
>
> Is this the wrong approach?
It's the wrong approach. Here are some things to work on:
1. highlight:withFrame:inView: is documented thus:
> Redraws the receiver with the specified highlight setting.
"The receiver" refers to the _whole cell._ The frame and view parameters are there because NSCells don't have their own drawing contexts, so they have to be told what the view context is, and how large the whole cell is to be. It's not for highlighting a portion of a cell.
2. "Points per character" is a concept that might, by a miracle, work for certain strings rendered in certain fonts (no, most "monospaced" fonts are _not_ safe). It's not a usable technique.
In short, don't construct or draw a highlight rectangle on your own unless your needs are _very_ particular. Text layout a ses raisons que la raison ne connait point. You probably don't need to get into it.
3. What you do instead depends on what sort of thing "currentDataTextBox" is. What class?
Actually, we don't care what class it is, because that would be choosing a tool before we know what your purpose is. Is it supposed to be editable, or static? Scrollable? Selectable? Deactivate-able? One line, or more? Decorated with things like borders?
4. By "highlight," do you mean _highlighting_ — rendering the string in some distinctive manner? Or do you mean _selecting_ — imposing user-interface state like what's copyable and where typed text will go?
5. The easiest way to do highlighting would be to work with an NSAttributedString and set and unset background-color attributes. NSTextField permits attributed content (see the methods inherited from NSControl). If you're editing, respond to controlTextDidChange: to keep the colored areas in sync.
6. For bigger, more flexible uses, use an NSTextView (be sure to read NSText, as well). It will give you scrolling, easier handling of paragraphs, and so on. If you have those very-particular needs, NSTextView will also afford you access to the whole Cocoa Text stack (look it up, and despair).
— F
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden