Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>UIScrollView</code> has the method <code>setContentOffset:animated:</code> which would allow you to scroll to a particular point in your content. To figure out what the appropriate offset is, you would have to determine the width of the portion of your attributed string prior to the highlighted part. The documentation for the methods you use to do this <a href="http://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html" rel="nofollow">can be found here</a>. You could do this using the <code>size</code> method of <code>NSAttributedString</code>.</p> <p>It would look something like this:</p> <pre><code>@interface SomeViewController : UIViewController @end @implementation SomeViewController { UIScrollView* _scrollView; } - (void)scrollToOffset:(NSInteger)offset inAttributedString:(NSAttributedString*)attributedString { NSAttributedString* attributedSubstring = [attributedString attributedSubstringFromRange:NSMakeRange(0, offset)]; CGFloat width = attributedSubstring.size.width; [_scrollView setContentOffset:CGPointMake(width, 0.0f) animated:YES]; } @end </code></pre> <p>The above code assumes that we're talking about a single line of text and that scrolls horizontally. Alternatively to do this for a fixed width that wraps to an arbitrary height you would need to calculate height with the following code (rather than attributedSubstring.size.height) and then possibly subtract a little to account for wanting to show the last line that of the substring:</p> <pre><code> CGFloat height = [attributedSubstring boundingRectWithSize:CGSizeMake(&lt;#fixed width to wrap at#&gt;, HUGE_VALF) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height; [_scrollView setContentOffset:CGPointMake(0.0f, height) animated:YES]; </code></pre> <p>This is similar to code I use when determining the height of table or collection view cells that have dynamic text that I need to accommodate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload