1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Getting the rects of a range inside an NSTextField

Discussion in 'Mac Programming' started by Starfox, Mar 13, 2012.

  1. macrumors regular

    #1
    How can I get the rect(s) of an NSRange in an NSTextField's attributed string value?
     
  2. macrumors G5

    gnasher729

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

    And the usual question: What is it that you actually want to achieve?
     
  3. macrumors regular

    #3
    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.
     
  4. macrumors 68000

    Sydde

    #4
    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.
     
  5. macrumors regular

    #5
    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/Screen Shot 2012-03-14 at 4.03.31 PM.png

    Any idea as to what I'm doing wrong?
     
  6. macrumors 68000

    Sydde

    #6
    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.
     
  7. macrumors regular

    #7
    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?
     
  8. macrumors 68000

    #8
    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
     
  9. macrumors regular

    #9
    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.
     

Share This Page