HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在UITextView中获取CGRect文本,以便用CALayer突出显示大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在UITextView中绘制透明的CALayer,以突出显示搜索中匹配的文本. @H_696_5@ @H_696_5@我已经找到了正确的方法来做到这一点,但仍然没有找到正确的坐标.我需要找到文本容器的来源.现在,我得到textView的原点,并通过它来抵消图层的原点:

@H_696_5@
NSRange match = [[[self textView]text]rangeOfString:@"preDict the future"];
NSRange glyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];

CGRect textRect = [manager boundingRectForGlyphRange:glyphRangE intextContainer:[[self textView]textContainer]];

CGPoint textViewOrigin = self.textView.frame.origin;
textRect.origin.x += (textViewOrigin.x / 2);
textRect.origin.y += (textViewOrigin.y / 2);


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

[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];
@H_696_5@如果我移动文本字段,或者如果我不将偏移量除以2,则得到以下结果:

@H_696_5@我想知道我是否在正确的轨道上,以及如何找到NSTextContainer对象的起源.

解决方法

要正确定位图层,只需将self.textView.textContainerInset.top添加到textRect.origin.y,而不是文本视图的原点. @H_696_5@ @H_696_5@但正如我在评论中所说,如果你的比赛跨越两条线,它将无法奏效.您可能需要设置匹配范围的背景颜色以突出显示它(使用attributedText属性),但是您无法添加圆角或阴影.

大佬总结

以上是大佬教程为你收集整理的ios – 在UITextView中获取CGRect文本,以便用CALayer突出显示全部内容,希望文章能够帮你解决ios – 在UITextView中获取CGRect文本,以便用CALayer突出显示所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。