首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextView contentOffset on iOS 7

UITextView contentOffset on iOS 7
EN

Stack Overflow用户
提问于 2013-10-01 11:41:01
回答 6查看 15.4K关注 0票数 16

我的应用程序中有一个UITextView。每次追加新字符串时,我都需要动态地更改它的内容偏移量。下面的代码在iOS 6和更早版本上工作得很好,但在iOS 7上却不行。

代码语言:javascript
复制
CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset:bottomOffset animated:YES]; 

你知道为什么吗?

比你还重要。

更新

UITextView没有按它应该设置的方式设置它的contentOffset,或者其他什么东西把它搞砸了。结果并不像预期的那样(在iOS 6或更早版本上是如何的)。

我也尝试过设置新的属性"textContainerInset“,但仍然没有结果.

代码语言:javascript
复制
self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)    

更新

正如我在didScroll委托方法中所看到的,当我传递内容偏移量的正值时,它滚动到(0,0)。例如,我将内容偏移量设置为(0,35),并将其滚动到(0,0).

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-11-26 11:03:22

好的,根据我的研究和其他答案,问题在于UITextView的内容高度,而不是滚动到特定的偏移量。下面是一个解决方案,它的工作方式应该适用于iOS 7:

首先,您需要像这样重新创建UITextView

代码语言:javascript
复制
    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内容高度:

代码语言:javascript
复制
- (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;
}

现在您可以设置内容偏移量:

代码语言:javascript
复制
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];

更新

我在NSAttributedString的苹果文档中看到了这一点:“NSAttributedString对象的默认字体是Helvetica 12点,这可能与平台的默认系统字体不同。”

总之,如果您使用不同大小的不同字体,则还必须将这些字体设置为NSAttributeString实例。否则,返回的高度将不符合您的期望。您可能想使用这样的东西:

代码语言:javascript
复制
    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];
票数 4
EN

Stack Overflow用户

发布于 2013-10-02 12:42:43

我找到的解决方案一点也不优雅,但它有效:

在将UITextView的contentOffset设置为所需的偏移量之前,我将其设置为(0,0)。我不知道这是为什么,但就目前而言,这是我找到的最好的解决方案。

代码语言:javascript
复制
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]; 
票数 8
EN

Stack Overflow用户

发布于 2014-03-10 10:11:16

我今天就注意到了。在我的例子中,这个问题显然与文本视图是视图中的第一个控件有关。我将其移到文档大纲中的第二位(例如,在UILabel之后),偏移量就消失了。

这很可能是故意的,因为iOS7中的导航栏有新的行为。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19115233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档