Ответ 1
Хорошо, на основании моих исследований и других ответов проблема заключалась в высоте содержимого UITextView
, а не прокрутке к определенному смещению. Вот решение, которое работает так, как должно работать для iOS 7:
Сначала вам нужно воссоздать UITextView
следующим образом:
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];
}
Затем используйте этот метод, чтобы получить высоту содержимого UITextView
:
- (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;
}
Теперь вы можете установить смещение содержимого:
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];
UPDATE
Я прочитал это в Apple Documentation для NSAttributedString
:
"Шрифт по умолчанию для объектов NSAttributedString - это 12-точечная версия Helvetica, которая может отличаться от системного шрифта по умолчанию для платформы".
В заключение, если вы используете разные шрифты разных размеров, вы должны также установить их в экземпляр NSAttributeString
. В противном случае возвращаемая высота не будет соответствовать вашим ожиданиям. Вы можете использовать что-то вроде этого:
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];