大佬教程收集整理的这篇文章主要介绍了ios – 使用CALayer突出显示跨越多行的UITextView中的文本,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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坐标).
在此屏幕截图中,“返回@L_675_6@的字符范围”应突出显示.
似乎文本不在返回或空格附近,这可以正常工作:
我错过了什么吗?
更新:
我已经把问题缩小了一点,我相信enumerateLineFragmentsForGlyphRange:usingBlock:正在跳过返回的行.
而不是使用enumerateEnclosingRectsForGlyphRange:我使用NSString方法enumerateSubStringsInRange:options:usingBlock:
我像往常一样枚举行片段,但是我没有尝试使用封闭的rect枚举方法,而是在构建图层的rect时枚举行上的每个字符.
-(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,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中的文本全部内容,希望文章能够帮你解决ios – 使用CALayer突出显示跨越多行的UITextView中的文本所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。