title: Using a CALayer to highlight text in a UITextView which spans multiple lines
body: <p>This is a continuation of <a href="https://stackoverflow.com/questions/20599431/using-nstextcontainer-and-nslayoutmanager-to-highlight-text-with-a-calayer">Getting CGRect for text in a UITextView for the purpose of highlighting with a CALayer</a>.  I'm having trouble with getting the correct rectangle for the ranges in each line fragment.</p>
  
  <pre><code>NSString* searchString = @"Returns the range of characters that generated";
  NSRange match = [[[self textView]text]rangeOfString:searchString];
  NSRange matchingGlyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];
  
  
  
  [manager enumerateLineFragmentsForGlyphRange:matchingGlyphRange usingBlock:
   ^(CGRect lineRect, CGRect usedRect, NSTextContainer *textContainer, NSRange lineRange, BOOL *stop) {
  
       NSRange currentRange = NSIntersectionRange(lineRange, matchingGlyphRange);
  
       [manager enumerateEnclosingRectsForGlyphRange:currentRange withinSelectedGlyphRange:NSMakeRange(NSNotFound, 0) inTextContainer:textContainer usingBlock:
        ^(CGRect rect, BOOL* stop) {
           if (usedRect.origin.y == rect.origin.y &amp;&amp; NSLocationInRange(currentRange.location, lineRange)) {
  
               CGRect theRect = [manager boundingRectForGlyphRange:currentRange inTextContainer:textContainer];
  
               CALayer* roundRect = [CALayer layer];
               [roundRect setFrame:theRect];
               [roundRect setBounds:theRect];
  
               [roundRect setCornerRadius:5.0f];
               [roundRect setBackgroundColor:[[UIColor blueColor]CGColor]];
               [roundRect setOpacity:0.2f];
               [roundRect setBorderColor:[[UIColor blackColor]CGColor]];
               [roundRect setBorderWidth:3.0f];
               [roundRect setShadowColor:[[UIColor blackColor]CGColor]];
               [roundRect setShadowOffset:CGSizeMake(20.0f, 20.0f)];
               [roundRect setShadowOpacity:1.0f];
               [roundRect setShadowRadius:10.0f];
  
               [[[self textView]layer]addSublayer:roundRect];
               *stop = YES;
           }
       }];
  }];
  </code></pre>
  
  <p>This my current attempt.  I'm basically using enumerateLineFragmentsForGlyphRange:usingBlock: to cycle through each line.  Then i'm using enumerateEnclosingRectsForGlyphRange:withinSelectedGlyphRange:usingBlock: to make sure the range of the text on the current line matches up with the line's range and has the same Y coordinate.  Sometimes this works, and sometimes it doesn't.</p>
  
  <p>It seems that if the search text is near a return it draws the highlight way off. (the y coordinate).</p>
  
  <p><img src="https://i.stack.imgur.com/bwBWE.png" alt="Highlight is off.  It seems to be the Y coordinate that is off"></p>
  
  <p>In this screenshot "Returns the range of characters that generated" is supposed to be highlighted.</p>
  
  <p>It seems if the text is not near a return or whitespace this works correctly:</p>
  
  <p><img src="https://i.stack.imgur.com/eP6XW.png" alt="Working Correctly if there is no whitespace around the text"></p>
  
  <p>Am I missing something?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've narrowed the problem down a bit, I believe enumerateLineFragmentsForGlyphRange: usingBlock: is skipping over lines which have returns.</p>