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

标签 objective-c ios7 uitextview

我已经阅读了该网站上的几个解决方案并尝试了所有这些解决方案,但仍然没有运气。目前我的测试是一个带有 UITextViewviewController 。我试图根据文本大小调整 UITextView 的高度。

好处:下面引用的解决方案似乎为我提供了在 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 中定义的框架。

最佳答案

这是一个可能对您有帮助的 github 存储库:resizableTextView

关于objective-c - 根据文本大小更改 UITextView 的高度 - ios7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318317/

相关文章:

ios - 内存管理。 CF 对象的字节副本

objective-c - 将文本与 UITableViewCell 对齐时出现问题

ios - 存折通行证在 iOS 7 中不起作用 - 仅在 iOS 6 中起作用

ios - UITextView 没有在 UICollectionViewCell 中启动

ios - Android 的 "ClickableSpan"类在 Swift/Objective-C 中的等效项是什么?

ios - 奇怪的 NSManagedObject 行为

ios - 谓词获取所有对象

ios - 获取相册列表的简单示例?

ios - 在哪里可以找到 iOS 用户体验导航最佳实践?

ios7 - UITextView 在 iOS 7 中错误地渲染自定义字体