我试过sizeWithFont:constrainedToSize:lineBreakMode
and sizeToFit
,两者都没有返回正确的结果。
该属性contentSize
应该返回正确的高度,但在文本视图呈现到屏幕之前它不起作用,并且我需要在文本视图可见之前计算高度(它确定UITableViewCell
.
有没有人找到任何其他方法,自定义文本视图等,可以正确计算 UITextView 的高度?
编辑 我应该澄清我想要基于文本视图内容的理想高度。
我试过sizeWithFont:constrainedToSize:lineBreakMode
and sizeToFit
,两者都没有返回正确的结果。
该属性contentSize
应该返回正确的高度,但在文本视图呈现到屏幕之前它不起作用,并且我需要在文本视图可见之前计算高度(它确定UITableViewCell
.
有没有人找到任何其他方法,自定义文本视图等,可以正确计算 UITextView 的高度?
编辑 我应该澄清我想要基于文本视图内容的理想高度。
IOS 7 不再支持该属性contentSize
。尝试使用这个
CGFloat textViewContentHeight = textView.contentSize.height;
textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9);
这解决了我的问题。
当涉及 sizeToFit: 和 layouSubview 的技术不起作用时,我使用这个:
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView
{
if ([textView respondsToSelector:@selector(snapshotViewAfterScreenUpdates:)])
{
// This is the code for iOS 7. contentSize no longer returns the correct value, so
// we have to calculate it.
//
// This is partly borrowed from HPGrowingTextView, but I've replaced the
// magic fudge factors with the calculated values (having worked out where
// they came from)
CGRect frame = textView.bounds;
// Take account of the padding added around the text.
UIEdgeInsets textContainerInsets = textView.textContainerInset;
UIEdgeInsets contentInsets = textView.contentInset;
CGFloat leftRightPadding = textContainerInsets.left + textContainerInsets.right + textView.textContainer.lineFragmentPadding * 2 + contentInsets.left + contentInsets.right;
CGFloat topBottomPadding = textContainerInsets.top + textContainerInsets.bottom + contentInsets.top + contentInsets.bottom;
frame.size.width -= leftRightPadding;
frame.size.height -= topBottomPadding;
NSString *textToMeasure = textView.text;
if ([textToMeasure hasSuffix:@"\n"])
{
textToMeasure = [NSString stringWithFormat:@"%@-", textView.text];
}
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle };
CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGFloat measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);
return measuredHeight;
}
else
{
return textView.contentSize.height;
}
}
这是对我有用的代码:
+ (CGFloat)heightOfComment:(NSString *)comment {
UILabel *label = PrototypeCell.commentLabel;
// NOTE: The height of the comment should always be at least the height of
// one line of text.
if (comment.length == 0)
comment = @" ";
return [comment sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:label.lineBreakMode].height;
}
让它发挥作用的关键是if (comment.length == 0) comment = @" ";
.
在 UITextview 布局之前,高度不可用。在 iOS 6 上,addSubview(例如,给 UIScrollView)赋予它布局值(即 frame.size.height)。在 iOS 7 上,这并不总是发生 - 对于 sizeToFit,同样如此。
我找到了一些解决方案,我更喜欢执行 sizeToFit 的解决方案,然后是 layoutIfNeeded,然后再阅读(现已更改)高度:
...
[scrollView1 addSubview: myTextView];
[myTextView sizeToFit]; //added
[myTextView layoutIfNeeded]; //added
CGRect frame = myTextView.frame;
...
这是来自此处接受的答案,请参阅注释。我在这里解释的尝试是在这种情况下,这对于添加到 UITableViewCells 而不是 UIScrollViews 的 UITextviews 应该无效(出于某种原因)。