【问题标题】:Searching and Auto-Scrolling UIScrollview with UILabel inside使用 UILabel 搜索和自动滚动 UIScrollview
【发布时间】:2015-06-03 00:47:06
【问题描述】:
我有一个 UIScrollView,里面有一个 UILabel,里面填充了非英文文本。
我想在滚动视图/UILabel 中搜索单词/短语并自动滚动到该点。
我知道没有简单的方法可以做到这一点,因为 UILabel 没有功能。
我不能使用 UITextView,因为由于某种原因, UITextView 上的常规滚动非常不稳定,这会导致糟糕的用户体验(来自用户的评论)。
UIScrollView 上的 UILabel 滚动非常流畅,但明显缺乏 UITextView 的能力。有任何想法吗?
【问题讨论】:
标签:
objective-c
uiscrollview
uilabel
【解决方案1】:
我从Luke Rogers 那里得到了我的回答,他回答了这个问题SO question
- (CGRect)boundingRectForCharacterRange:(NSRange)range
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:[self attributedText]];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self 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];
}