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

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

问题描述

我的应用中有一个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];

您有什么想法吗?



比你。



UPDATE



UITextView未设置其contentOffset因为它应该,或其他东西搞砸了。结果不如预期(iOS 6或更早版本的版本)。



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

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

更新



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

解决方案

好的,根据我的研究和其他答案,问题是 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];
//如果使用ARC,删除这3行
[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);

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

更新



<我已在Apple文档中读到这个 NSAttributedString
NSAttributedString对象的默认字体是Helvetica 12-point,这可能与默认字节不同平台的系统字体。



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

  NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] //或任何其他字体或大小
forKey:NSFontAttributeName];
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:currentPost.postMessageText属性:attrsDictionary];
frame.size.height = [self textViewHeightForAttributedText:attributedString andWidth:280.0];
[attributionString release];


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

Do you have any ideas why?

Than you.

UPDATE

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).

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

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

UPDATE

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)...

解决方案

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:

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

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

UPDATE

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."

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天全站免登陆