layoutManager boundingRectForGlyphRange:inTextContainer:不适用于所有字符串

编程入门 行业动态 更新时间:2024-10-18 12:30:03
本文介绍了layoutManager boundingRectForGlyphRange:inTextContainer:不适用于所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个UILabel,带有类似字符串的推文,包括其他用户的提及.

I have a UILabel with a tweet like string, including mentions of other users.

Hey @stephen and @frank and @Jason1.

我正在尝试使每个提及都可以点击,以便我可以加载该用户的个人资料.我从另一篇SO帖子中找到了一些代码(如何在UILabel中找到文本子字符串的CGRect?),我可以使用该CGRect来定位字符串中每个提及的位置.但是,对于帖子中的最后2条或最后2条提及,通常不起作用.

I am attempting to have each mention be tappable so I can load that user's profile. I found some code from another SO post (How do I locate the CGRect for a substring of text in a UILabel?) that I was able to use in order to locate the position of each mention in the string. However it usually doesn't work for the last or last 2 mentions in a post.

SO帖子中的方法(稍作修改):

The method from the SO post (slightly modified):

- (CGRect)boundingRectForCharacterRange:(NSRange)range { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.myLabel.attributedText]; NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString]; NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.myLabel.bounds.size]; textContainer.lineFragmentPadding = 0; [layoutManager addTextContainer:textContainer]; NSRange glyphRange; // Convert the range for glyphs. [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange]; return [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]; }

然后,在 touchesEnded:中,我遍历每个提及内容,获取主字符串中的范围,并检查触摸是否在该CGRect内部.

Then, in touchesEnded:, I loop over each mention, get the range in the main string, and check if the touch is inside that CGRect.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = touches.allObjects[0]; for (NSString *mention in self.mentions) { NSRange range = [self.postText rangeOfString:mention options:NSCaseInsensitiveSearch]; CGRect rect = [self boundingRectForCharacterRange:range]; NSLog(@"rect for %@ is %@", mention, NSStringFromCGRect(rect)); } } // Output from above rect for @stephen is {{33.388, 0}, {72.471001, 20.553001}} rect for @frank is {{143.021, 0}, {49.809998, 20.553001}} rect for @Jason1 is {{0, 0}, {0, 0}}

这在大多数情况下都有效,但是@ Jason1没有匹配.我已经切换了名称的顺序,并且始终是最后一个.我的标签确实会自动换行,但有时仍会与第二行和第三行上的名称匹配.是否有设置或我缺少的东西?我试过更改标签的大小和字体,但是没有运气.我在这里真是茫然.

And this works great most of the time, however @Jason1 does not get matched. I've switched the order of the names and it's always the last one. My label does wrap, but it still sometimes matches names on the 2nd and 3rd lines. Is there a setting or something I'm missing? I've tried changing the size and font of the labels but no luck. I'm at a real loss here.

推荐答案

此问题的解决方法是在初始化NSTextContainer时不设置高度约束.改用非常大的数字.

The fix for this is not to set the height constraint when initializing the NSTextContainer. Use a very large number instead.

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(self.myLabel.bounds.size.width, CGFLOAT_MAX)];

更多推荐

layoutManager boundingRectForGlyphRange:inTextContainer:不适用于所有字符串

本文发布于:2023-11-30 17:36:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不适用于   layoutManager   boundingRectForGlyphRange   inTextContainer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!