iOS 7 上的 UITextView contentOffset [英] UITextView contentOffset on iOS 7

查看:13
本文介绍了iOS 7 上的 UITextView contentOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个 UITextView.每次附加新字符串时,我都需要动态更改它的内容偏移量.下面的代码在 iOS 6 及更早版本上运行良好,但不适用于 iOS 7.

I have a UITextView in my app. I need to change it's content offset dynamically every time a new string is appended. The code bellow works fine on iOS 6 and earlier versions, but not on iOS 7.

CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset:bottomOffset animated:YES]; 

你知道为什么吗?

比你.

更新

UITextView 没有按照应有的方式设置它的 contentOffset,或者其他什么东西把它搞砸了.结果与预期不符(iOS 6 或更早版本的情况).

The UITextView doesn't set its contentOffset as it should, or something else mess it up. The result is not as expected (how it is on iOS 6 or earlier versions).

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

I've also tried setting the new property "textContainerInset" but still no results...

self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)    

更新

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

As I can see in the didScroll delegate method, when I pass a positive value for content offset, it scrolls to (0,0). For example, I set the content offset to (0,35) and it scrolls to (0,0)...

推荐答案

好的,根据我的研究和其他答案,问题是 UITextView 的内容高度,而不是滚动到特定的抵消.这是一个适用于 iOS 7 的解决方案:

Ok, based on my research and other answers, the problem was the UITextView'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:

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

First, you need to recreate the UITextView like this:

    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的内容高度:

Then, use this method to get the UITextView content height:

- (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;
}

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

Now you can set the content offset:

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 的 Apple 文档中读到了这个:NSAttributedString 对象的默认字体是 Helvetica 12 号字体,可能与平台的默认系统字体不同."

I've read this in the Apple Documentation for NSAttributedString: "The default font for NSAttributedString objects is Helvetica 12-point, which may differ from the default system font for the platform."

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

In conclusion, if you use different fonts of different sizes, you have to set those to the NSAttributeString instance as well. Otherwise, the returned height won't correspond to your expectations. You may want to use something like this:

    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];

这篇关于iOS 7 上的 UITextView contentOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆