
我已经找到了正确的方法来做到这一点,但仍然没有找到正确的坐标.我需要找到文本容器的来源.现在,我得到textVIEw的原点,并通过它来抵消图层的原点:
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];
如果我移动文本字段,或者如果我不将偏移量除以2,则得到以下结果:
我想知道我是否在正确的轨道上,以及如何找到NSTextContainer对象的起源.
解决方法 要正确定位图层,只需将self.textVIEw.textContainerInset.top添加到textRect.origin.y,而不是文本视图的原点.但正如我在评论中所说,如果你的比赛跨越两条线,它将无法奏效.您可能需要设置匹配范围的背景颜色以突出显示它(使用attributedText属性),但是您无法添加圆角或阴影.
总结以上是内存溢出为你收集整理的ios – 在UITextView中获取CGRect文本,以便用CALayer突出显示全部内容,希望文章能够帮你解决ios – 在UITextView中获取CGRect文本,以便用CALayer突出显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)