UITextView 内容大小太短 [英] UITextView Content Size too Short

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

问题描述

我有一个 UITextView,我想根据其内容动态调整大小.

I have a UITextView which I would like to dynamically resize dependant on its content.

出于某种原因,textview 总是比它的内容短!代码如下:

For some reason the textview is always shorter that its contents! Here's the code:

_textView.text = [NSString stringWithFormat:@"Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test "];

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

结果如下:

如您所见,UITextView(白色)太短了.如果我把更多或更少的内容放在那里,简短是一样的,即

As you can see the UITextView (in white) is too short. If I put more or less contents in there the shortness is the same, i.e.

我确定这很简单,但我无法解决.

I'm sure it's something simple but I can't work it out.

推荐答案

我在 iOS7 中使用故事板时遇到了同样的问题.

I had this same problem using storyboards in iOS7.

我的解决方案是添加此代码:

My solution was to add this code:

    CGRect newTextViewFrame = self.textView.frame;
    newTextViewFrame.size.width = self.textView.contentSize.width + self.textView.contentInset.right + self.textView.contentInset.left;
    newTextViewFrame.size.height = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
    self.textView.frame = newTextViewFrame;

viewDidAppear: 视图生命周期方法中,它会立即调整大小以适合其内容.不幸的是,这种调整大小发生在视图已经出现之后,这意味着用户看到它正在发生.

in the viewDidAppear: view life cycle method and it was immediately sized to fit it's contents. Unfortunately, this resize happens after the view has already appeared which means that the user sees it happening.

示例:

- (void)viewDidAppear:(BOOL)animated
{
        [super viewDidAppear:animated];

        CGRect newTextViewFrame = self.textView.frame;
        newTextViewFrame.size.width = self.textView.contentSize.width + self.textView.contentInset.right + self.textView.contentInset.left;
        newTextViewFrame.size.height = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
        self.textView.frame = newTextViewFrame;
}

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

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