2013-10-11 49 views
3

我已經閱讀了本網站上的幾個解決方案並嘗試了所有這些解決方案,但仍然沒有運氣。目前我的測試是一個viewController,其上有一個UITextView。我正在嘗試根據文字大小調整我的UITextView的高度。根據文本大小更改UITextView的高度 - ios7

好:下面引用的解決方案似乎給了我在UITextView中設置的正確高度。

Link

壞:獲得理想的高度回來後,似乎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中定義的幀。

回答