0

I have a UIScrollView with a UILabel inside of it populated with non-english text.

I would like to search the scrollview/UILabel for a word/phrase and auto-scroll to that point.

I know theres no simple way to do this because theres no function for UILabel. I can't use a UITextView because for some reason the regular scrolling on the UITextView is very choppy which makes a bad user experience (reviews from users). UILabel on UIScrollView scrolls extremely smoothly but clearly lacks the ability of UITextView. Any ideas?

huddie96
  • 1,240
  • 2
  • 13
  • 26

1 Answers1

1

I got my answer from Luke Rogers who answered this 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];
}
Community
  • 1
  • 1
huddie96
  • 1,240
  • 2
  • 13
  • 26