Objective c 根据文本大小更改UITextView的高度-ios7

Objective c 根据文本大小更改UITextView的高度-ios7,objective-c,ios7,uitextview,Objective C,Ios7,Uitextview,我在这个网站上读过一些解决方案,并尝试了所有的解决方案,但仍然没有成功。目前,我的测试是一个单一的viewController,上面有一个UITextView。我正在尝试根据文本大小调整我的UITextView的高度 好:下面引用的解决方案似乎给了我在我的UITextView中设置的正确高度 “” 错误:恢复理想高度后,UITextView似乎不允许设置高度。下面的代码显示了我为实现这一点所做的几种不同尝试。目前,第三个try方法似乎是最接近的,但它仍然给我留下了一个固定的高度UITextVie

我在这个网站上读过一些解决方案,并尝试了所有的解决方案,但仍然没有成功。目前,我的测试是一个单一的
viewController
,上面有一个
UITextView
。我正在尝试根据文本大小调整我的
UITextView
的高度

好:下面引用的解决方案似乎给了我在我的
UITextView
中设置的正确高度

“”

错误:恢复理想高度后,
UITextView
似乎不允许设置高度。下面的代码显示了我为实现这一点所做的几种不同尝试。目前,第三个try方法似乎是最接近的,但它仍然给我留下了一个固定的高度
UITextView

这是我在第三季度的最新日志的输出。看起来一切正常,但是
UITextView
没有调整大小

2013-10-11 08:27:03.374 textexpand[25273:a0b] The bounds height 872.000000
2013-10-11 08:27:03.377 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.378 textexpand[25273:a0b] The frame height 872.000000
2013-10-11 08:27:03.380 textexpand[25273:a0b] The frame height 785.000000
2013-10-11 08:27:03.381 textexpand[25273:a0b] The bounds height 785.000000
代码如下:

- (void)viewDidLoad
{
[super viewDidLoad];

[self thirdtry];

}
////

////

////

////

更新:

- (void)viewDidLoad
{
[super viewDidLoad];

[self thirdtry];

}
我应该补充一点,通过使用以下方法在代码中初始化uitextbox,可以获得所需的效果:

CGFloat myfloat= [self textViewHeightForAttributedText:attText andWidth:300];
_theTextView = [[UITextView alloc] initWithFrame:CGRectMake(350, 100, 300, myfloat)];
_theTextView.text = theText;
[_theview addSubview:_theTextView];

但是,我不希望这样做,而是动态调整IB中定义的帧。

这里有一个github回购协议,可以帮助您:

- (void) thirdtry
{

_theTextView.scrollEnabled = NO;

NSString *theText = @"Here is the text I am going to load into the textview.   The text view should automatically expand the height based on the size of the content, but leave the width static.   Also, I would ideally add my own padding to the bottom and top of the uitext view. More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text More text";

NSAttributedString *attText = [[NSAttributedString alloc] initWithString:theText attributes:nil];

//Attempt to adjust the bounds
CGRect bounds = _theTextView.bounds;
bounds.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The bounds height %f", bounds.size.height);
_theTextView.bounds = bounds;

//Attempt to adjust the frame
CGRect frame = _theTextView.frame;
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;
NSLog(@"The frame height %f", frame.size.height);

NSLog(@"The frame height %f", _theTextView.frame.size.height);
[_theTextView sizeToFit];
NSLog(@"The frame height %f", _theTextView.frame.size.height);
}
- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width
{
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:text];
CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
return size.height;

}
CGFloat myfloat= [self textViewHeightForAttributedText:attText andWidth:300];
_theTextView = [[UITextView alloc] initWithFrame:CGRectMake(350, 100, 300, myfloat)];
_theTextView.text = theText;
[_theview addSubview:_theTextView];