Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, based on my research and other answers, the problem was the <code>UITextView</code>'s content height and not the scrolling to a specific offset. Here is a solution that works the way it should work for iOS 7:</p> <p>First, you need to recreate the <code>UITextView</code> like this:</p> <pre><code> NSString *reqSysVer = @"7.0"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); if (osVersionSupported) { NSLog(@"reset chatoutput"); CGRect outputFrame = self.chatOutput.frame; [chatOutput removeFromSuperview]; [chatOutput release]; chatOutput = nil; NSTextStorage* textStorage = [[NSTextStorage alloc] init]; NSLayoutManager* layoutManager = [NSLayoutManager new]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size]; [layoutManager addTextContainer:textContainer]; chatOutput = [[UITextView alloc] initWithFrame: outputFrame textContainer: textContainer]; // if using ARC, remove these 3 lines [textContainer release]; [layoutManager release]; [textStorage release]; [self.view addSubview: chatOutput]; } </code></pre> <p>Then, use this method to get the <code>UITextView</code> content height:</p> <pre><code>- (CGFloat)textViewHeightForAttributedText:(NSAttributedString*)text andWidth:(CGFloat)width { UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAttributedText:text]; CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)]; NSLog(@"size: %f", size.height) ; return size.height; } </code></pre> <p>Now you can set the content offset:</p> <pre><code>CGPoint bottomOffset; bottomOffset = CGPointMake(0, [self textViewHeightForAttributedText: self.chatOutput.attributedText andWidth: self.chatOutput.frame.size.width] - self.chatOutput.frame.size.height); [self.chatOutput setContentOffset:bottomOffset animated:YES]; </code></pre> <p><strong>UPDATE</strong></p> <p>I've read this in the Apple Documentation for <code>NSAttributedString</code>: "The default font for NSAttributedString objects is Helvetica 12-point, which may differ from the default system font for the platform."</p> <p>In conclusion, if you use different fonts of different sizes, you have to set those to the <code>NSAttributeString</code> instance as well. Otherwise, the returned height won't correspond to your expectations. You may want to use something like this:</p> <pre><code> NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject: [UIFont systemFontOfSize: 18.0] //or any other font or size forKey: NSFontAttributeName]; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: currentPost.postMessageText attributes: attrsDictionary]; frame.size.height = [self textViewHeightForAttributedText: attributedString andWidth: 280.0]; [attributedString release]; </code></pre>
 

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