我尝试了sizeWithFont:constrainedToSize:lineBreakMode
和sizeToFit
,两者都没有返回正确的结果。
属性contentSize
应该返回正确的高度,但是在文本视图呈现到屏幕之前它不起作用,我需要在文本视图可见之前计算高度(它确定UITableViewCell
的高度。
有没有人找到正确计算UITextView高度的任何其他方法,自定义文本视图等?
修改 我应该根据文本视图的内容澄清我想要理想的高度。
答案 0 :(得分:1)
IOS 7不再支持属性contentSize
。尝试使用此
CGFloat textViewContentHeight = textView.contentSize.height;
textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9);
这解决了我的问题。
答案 1 :(得分:0)
以下代码对我有用:
+ (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 = @" ";
。
答案 2 :(得分:0)
当涉及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;
}
}
答案 3 :(得分:-2)
在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;
...
这是来自接受的答案here,请参阅说明。我在这里尝试解释的情况是,这对于添加到UITableViewCells而不是UIScrollViews的UITextviews应该是无效的(出于某种原因)。