我想根据文本长度增加textview高度我尝试了一些代码,但结果就像第一张图片一样,但我想像第二张图片一样得到结果。
码
- (void)viewDidLoad {
[super viewDidLoad];
textview.text = @"This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode also.This returns the size of the rectangle that fits the given string with the given font. Pass in a size with the desired width and a maximum height, and then you can look at the height returned to fit the text. There is a version that lets you specify line break mode yes.";
CGRect frame = textview.frame;
frame.size.height = textview.contentSize.height;
textview.frame = frame;
textview.ScrollEnabled = NO;
}
答案 0 :(得分:1)
答案 1 :(得分:1)
看看this example。基本思想是从控制器中隔离调整大小代码。示例中的代码可以重写:
override func updateConstraints() {
let contentSize = sizeThatFits(CGSizeMake(frame.size.width, CGFloat.max))
for constraint in constraints() {
if constraint.isKindOfClass(NSLayoutConstraint) {
let constraint = constraint as! NSLayoutConstraint
if constraint.firstAttribute == NSLayoutAttribute.Height {
constraint.constant = contentSize.height
}
}
}
super.updateConstraints()
}
答案 2 :(得分:0)
如果您正在使用约束(并且您应该),则不要设置任何高度约束并禁用滚动。 这将根据内容自动更新文本视图的高度。