根据文本大小改变UITextView的高度 - ios7

3

我已经阅读了这个网站上的几种解决方案并尝试了它们,但仍然没有成功。目前我的测试是一个单一的viewController,其中包含一个UITextView。我正在尝试根据文本大小调整UITextView的高度。

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

"链接"

坏处:在得到理想的高度后,似乎UITextView不允许设置高度。下面的代码显示了我尝试过的几种不同方法。目前第三种尝试方法似乎最接近,但仍然留下了一个固定高度的UITextViewenter image description here

这是我最近从第三次尝试的日志输出。看起来一切都正常,但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) firsttry
{
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";

_theTextView.text = theText;

_theTextView.scrollEnabled = NO;
[_theTextView sizeToFit];
[_theTextView layoutIfNeeded];

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

}

////

- (void) secondtry
{
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];
CGRect frame = _theTextView.frame;
CGFloat myfloat = [self textViewHeightForAttributedText:attText andWidth:100];
NSLog(@"The float is %f", myfloat);
frame.size.height = [self textViewHeightForAttributedText:attText andWidth:100];
_theTextView.frame = frame;


}

////

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

}

更新:

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

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

然而,我更希望不要这样做,并动态调整IB中定义的框架。
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接