
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坐标).
在此屏幕截图中,“返回生成的字符范围”应突出显示.
似乎文本不在返回或空格附近,这可以正常工作:
我错过了什么吗?
更新:
我已经把问题缩小了一点,我相信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 stringNSRange match = [[[self textVIEw]text]rangeOfString:string options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch | NSWIDthInsensitiveSearch];// Convert it to a glyph rangeNSRange 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.@R_403_6823@; 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中的文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)