我将NSAttributedString传递给UITextView:
NSAttributedString *str = @"A really long string ... that continues for awhile";
字符串的长度是随机的,我想将它传递给UITextView。
myTextView.attributedText = str;
我需要UITextView的宽度为300但是如何找到heigt?
答案 0 :(得分:2)
如果我没弄错,你可以这样做:
[myTextView sizeToFit];
然后从myTextView.bounds.size.height
检索高度。请注意,UITextView
也会在边缘添加一些填充,因此您可能希望从高度中减去该填充。
如果我弄错了,那么这是一个不太干净但可行的替代方案:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300.f, yourMaxHeight)];
label.attributedText = myTextView.attributedText;
label.numberOfLines = INFINITY;
[label sizeToFit];
CGFloat height = label.bounds.size.height;
这当然不包括UITextView
填充。