Mih*_*ete 16 iphone uitextview ios7
我的应用程序中有一个UITextView.每次添加新字符串时,我都需要动态更改内容偏移量.代码bellow在iOS 6及更早版本上运行良好,但在iOS 7上运行不正常.
CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset:bottomOffset animated:YES];
Run Code Online (Sandbox Code Playgroud)
你有什么想法吗?
比你.
UPDATE
UITextView没有设置它应该的contentOffset,或者其他东西弄乱了它.结果与预期不符(iOS 6或更早版本的版本).
我也试过设置新属性"textContainerInset"但仍然没有结果......
self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)
UPDATE
正如我在didScroll委托方法中看到的,当我为内容偏移传递正值时,它会滚动到(0,0).例如,我将内容偏移设置为(0,35)并滚动到(0,0)...
我发现的解决方案根本不优雅,但它有效:
在将UITextView的contentOffset设置为所需的偏移量之前,我将其设置为(0,0).我不知道为什么会这样,但现在是我找到的最好的解决方案.
CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset: CGPointMake(0,0) animated:NO];
[self.textView setContentOffset:bottomOffset animated:YES];
Run Code Online (Sandbox Code Playgroud)
小智 5
我今天才注意到.在我的情况下,问题显然与文本视图是视图中的第一个控件有关.我把它移到文档大纲中的第二位(例如在UILabel之后)并且偏移消失了.
最有可能这是因为iOS7中的导航栏新行为而故意完成的.
好的,根据我的研究和其他答案,问题是 的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];
}
Run Code Online (Sandbox Code Playgroud)
然后,使用此方法获取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;
}
Run Code Online (Sandbox Code Playgroud)
现在您可以设置内容偏移量:
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];
Run Code Online (Sandbox Code Playgroud)
更新
我在 Apple 文档中读到过这样的内容NSAttributedString:“NSAttributedString 对象的默认字体是 Helvetica 12 点,这可能与平台的默认系统字体不同。”
总之,如果您使用不同大小的不同字体,则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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15009 次 |
| 最近记录: |