ios - 使用 CALayer 突出显示跨多行的 UITextView 中的文本

标签 ios textkit nslayoutmanager nstextcontainer

这是 Getting CGRect for text in a UITextView for the purpose of highlighting with a CALayer 的延续.我无法为每个线段中的范围获取正确的矩形。

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 && 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;
         }
     }];
}];

这是我目前的尝试。我基本上使用 enumerateLineFragmentsForGlyphRange:usingBlock: 来循环遍历每一行。然后我使用 enumerateEnclosingRectsForGlyphRange:withinSelectedGlyphRange:usingBlock: 来确保当前行上的文本范围与该行的范围匹配并且具有相同的 Y 坐标。有时这行得通,有时行不通。

看起来,如果搜索文本靠近回车,它会将高亮显示远离。 (y 坐标)。

Highlight is off.  It seems to be the Y coordinate that is off

在此屏幕截图中,“返回生成的字符范围”应该突出显示。

似乎如果文本不在回车符或空格附近,它就可以正常工作:

Working Correctly if there is no whitespace around the text

我错过了什么吗?

更新:

我已经缩小了问题的范围,我相信 enumerateLineFragmentsForGlyphRange: usingBlock: 正在跳过有返回的行。

最佳答案

我找到了解决方案:

我没有使用 enumerateEnclosingRectsForGlyphRange: 我使用 NSString 方法 enumerateSubstringsInRange:options:usingBlock:

我像往常一样枚举行片段,但我没有尝试使用封闭矩形枚举方法,而是在为层构建矩形时枚举行中的每个字符。

-(void)drawLayerForTextHighlightWithString:(NSString*)string {

for (CALayer* eachLayer in [self highlightLayers]) {
    [eachLayer removeFromSuperlayer];
}

NSLayoutManager* manager = [[self textView]layoutManager];

// Find the string
NSRange match = [[[self textView]text]rangeOfString:string options:
                 NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWidthInsensitiveSearch];

// Convert it to a glyph range
NSRange matchingGlyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];

// Enumerate each line in that glyph range (this will fire for each line that the match spans)
[manager enumerateLineFragmentsForGlyphRange:matchingGlyphRange usingBlock:
 ^(CGRect lineRect, CGRect usedRect, NSTextContainer *textContainer, NSRange lineRange, BOOL *stop) {

     // currentRange uses NSIntersectionRange to return the range of the text that is on the current line
     NSRange currentRange = NSIntersectionRange(lineRange, matchingGlyphRange);

     // This rect will be built by enumerating each character in the line, and adding to it's width
     __block CGRect finalLineRect = CGRectZero;

     // Here we use enumerateSubstringsInRange:... to go through each glyph and build the final rect for the line
     [[[self textView]text]enumerateSubstringsInRange:currentRange options:NSStringEnumerationByComposedCharacterSequences usingBlock:
      ^(NSString* substring, NSRange substringRange, NSRange enclostingRange, BOOL* stop) {

          // The range of the single glyph being enumerated
          NSRange singleGlyphRange =  [manager glyphRangeForCharacterRange:substringRange actualCharacterRange:NULL];

          // get the rect for that glyph
          CGRect glyphRect = [manager boundingRectForGlyphRange:singleGlyphRange inTextContainer:textContainer];

          // check to see if this is the first iteration, if not add the width to the final rect for the line
          if (CGRectEqualToRect(finalLineRect, CGRectZero)) {
              finalLineRect = glyphRect;
          } else {
              finalLineRect.size.width += glyphRect.size.width;
          }

      }];

     // once we get the rect for the line, draw the layer
     UIEdgeInsets textContainerInset = [[self textView]textContainerInset];
     finalLineRect.origin.x += textContainerInset.left;
     finalLineRect.origin.y += textContainerInset.top;

     CALayer* roundRect = [CALayer layer];
     [roundRect setFrame:finalLineRect];
     [roundRect setBounds:finalLineRect];

     [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];
     [[self highlightLayers]addObject:roundRect];

     // continues for each line
 }];

我仍在处理多个匹配项,一旦我开始工作,我会更新代码。

关于ios - 使用 CALayer 突出显示跨多行的 UITextView 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20694942/

相关文章:

ios - 如何将广告合并到 libGDX 中

ios - 在单元格中编辑 UITextView 时,iOS 7 中的 UITableView 不会滚动到正确的位置

objective-c - 使用 NSLayoutManager 停止文本换行

ios - 如何读取firebase数据库并将其放入tableview单元格问题

ios - 无论如何检查 CloudKit 应用程序以开发人员或生产模式运行?

ios - 使用 Text Kit 将基线与大行高的字符对齐

ios - NSLayoutManager 每段有一个 NSTextContainer

ios - 使用 NSLayoutManager 创建动画文字效果?

objective-c - 如何获取 -textStorageDidProcessEditing :? 内可见的字符范围

ios - 在 WKWebView 中打开 Vimeo 视频