PDA

View Full Version : Getting the rects of a range inside an NSTextField




Starfox
Mar 13, 2012, 10:57 AM
How can I get the rect(s) of an NSRange in an NSTextField's attributed string value?



gnasher729
Mar 13, 2012, 11:06 AM
How can I get the rect(s) of an NSRange in an NSTextField's attributed string value?

The first step would be to say _exactly_ what you mean.

And the usual question: What is it that you actually want to achieve?

Starfox
Mar 13, 2012, 02:28 PM
I want to draw an overlay over a range of characters in an attributed string in a text field. I'm subclassing the textfield and I want to add a rect fill for the rect of characters in range R.

Sydde
Mar 13, 2012, 02:56 PM
You might want to investigate

Core Text (http://developer.apple.com/documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html)

It is a bit like working with Core Foundation (C-level, not Objective-C), but seems tailored for exactly what you are doing.

Starfox
Mar 14, 2012, 03:30 PM
Core text seems too low-level. I found a method that does what I want in NSLayoutManager (rectArrayForCharacterRange:withinSelectedCharacterRange:inTextContainer:rectCount), but it seems I'm not using it properly. The rects I get aren't aligned with the text. Here's my added code in my NSTextField subclass's drawRect right after a call to super's draw rect:


NSLog(@"Filling rects!");
///////
NSLayoutManager* LM = [[NSLayoutManager alloc] init];
NSAttributedString* AS = [self attributedStringValue];
NSTextStorage* TS = [[NSTextStorage alloc] initWithAttributedString:AS];
[TS setAttributedString:AS];
[LM setTextStorage:TS];
[TS addLayoutManager:LM];
NSTextContainer* TC = [[NSTextContainer alloc] initWithContainerSize:[self frame].size];
[LM addTextContainer:TC];
NSUInteger rectCount = 0;
NSRectArray linkRect = [LM rectArrayForCharacterRange:NSMakeRange(0, 2)
withinSelectedCharacterRange:NSMakeRange(0, 0)
inTextContainer:TC
rectCount:&rectCount];
activeURLRects.clear();
for(size_t i = 0; i < rectCount; ++i)
{
cout << "Rect:\n";
cout << "\t" << linkRect[i].size.width << " , " << linkRect[i].size.height << endl;
activeURLRects.push_back(linkRect[i]);
}
///////
for(size_t i = 0; i < activeURLRects.size(); ++i)
{
NSRect rect = activeURLRects[i];
NSColor* color = [NSColor colorWithDeviceRed:0.0 green:1.0 blue:0.0 alpha:0.3];
[color set];
NSRectFill(rect);
}


The rects are off by a few pixels:

http://dl.dropbox.com/u/24735880/Screen%20Shot%202012-03-14%20at%204.03.31%20PM.png

Any idea as to what I'm doing wrong?

Sydde
Mar 14, 2012, 09:11 PM
As I look at it, NSAttributedString has some built-in measuring tools (AppKit additions) that you could use without having to resort to creating a layout manager, text container, etc. (In looking at the docs for NSTextContainer, I see that it wants to belong to a NSTextView, which is kind of missing in your code.) You could create substrings to measure the location of the fragment you want to highlight: one to measure from the start of the line to where you want to highlight (if your range start might not be zero) and one to measure the size of the box.

Be sure that you are measuring in the correct graphic context (that focus is locked on your field, which may be automatic depending on the method you are in). I suspect the discrepancy you are experiencing may have to do with the fact that your get container is not in any view, so its co÷rdinates do not quite match with your field's view.

Starfox
Mar 15, 2012, 10:03 AM
Creating substrings sounds extremely inefficient - I'm going to have 5 ranges that I need to highlight in every string or so. I tried using my code with the layout manager of a text view and it worked fine, so the basic theory is OK. What am I doing with regards to setting up the layout manager that's wrong?

robvas
Mar 15, 2012, 10:48 AM
Google Chrome has the open source Chromium project - Maybe look in there to see how they highlight what you're searching for when you hit Command-F

Starfox
Mar 16, 2012, 04:33 PM
Google Chrome has the open source Chromium project - Maybe look in there to see how they highlight what you're searching for when you hit Command-F

Or I could just go around searching for needles in haystacks.

Seriously though, I wrote a simple project using code from Apple's docs and put it on BitBucket here:

https://bitbucket.org/sherief/textview

Would anyone mind taking a look at it and telling me where I went wrong? The code for getting the rects of a range inside the text view works fine, but the text field one is messed up. You'll see how the rectangle is offset once you build and run the project.