Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 13, 2012, 10:57 AM   #1
Starfox
macrumors regular
 
Join Date: Apr 2011
Getting the rects of a range inside an NSTextField

How can I get the rect(s) of an NSRange in an NSTextField's attributed string value?
Starfox is offline   0 Reply With Quote
Old Mar 13, 2012, 11:06 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Starfox View Post
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?
gnasher729 is offline   0 Reply With Quote
Old Mar 13, 2012, 02:28 PM   #3
Starfox
Thread Starter
macrumors regular
 
Join Date: Apr 2011
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.
Starfox is offline   0 Reply With Quote
Old Mar 13, 2012, 02:56 PM   #4
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
You might want to investigate

Core Text

It is a bit like working with Core Foundation (C-level, not Objective-C), but seems tailored for exactly what you are doing.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is online now   0 Reply With Quote
Old Mar 14, 2012, 03:30 PM   #5
Starfox
Thread Starter
macrumors regular
 
Join Date: Apr 2011
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:

Code:
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/Scr...03.31%20PM.png

Any idea as to what I'm doing wrong?
Starfox is offline   0 Reply With Quote
Old Mar 14, 2012, 09:11 PM   #6
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is online now   0 Reply With Quote
Old Mar 15, 2012, 10:03 AM   #7
Starfox
Thread Starter
macrumors regular
 
Join Date: Apr 2011
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?
Starfox is offline   0 Reply With Quote
Old Mar 15, 2012, 10:48 AM   #8
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: USA
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
robvas is offline   0 Reply With Quote
Old Mar 16, 2012, 04:33 PM   #9
Starfox
Thread Starter
macrumors regular
 
Join Date: Apr 2011
Quote:
Originally Posted by robvas View Post
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.
Starfox is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Using NSTextField to save text into cutom format at a temp location SirFurtWiggle Mac Programming 1 Apr 24, 2014 09:16 AM
How do you set cursor initial cursor position and NSTextField bounds? DavidBlack Mac Programming 3 Mar 1, 2014 11:46 AM
NSTextField focus don't work if is added programmatically Ivang Mac Programming 5 Oct 1, 2013 09:27 AM
Adding a percent in a NSTextField jweinraub Mac Programming 13 May 12, 2013 11:27 PM
Have a NSTextField Popup on button click hightekrednek Mac Programming 0 Aug 7, 2012 07:41 PM

Forum Jump

All times are GMT -5. The time now is 03:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC