2012-05-14 51 views
5

我试过sizeWithFont:constrainedToSize:lineBreakModesizeToFit,它们都没有返回正确的结果。有没有办法可靠地计算UITextView的高度?

属性contentSize应该返回正确的高度,但它不起作用,直到文本视图呈现到屏幕上,我需要计算高度之前的文本视图是可见的(它决定了高度一个UITableViewCell

有没有人找到任何其他方法,自定义文本视图或类似,即正确地计算一个UITextView的高度?

编辑 我要澄清,我想基于理想的高度文本视图的内容。

+1

UITextView.frame.size.height? –

+0

这确实给了我们一个UITextView的高度,但我正在根据它的内容寻找理想的高度。我会更新我的问题。 – Quentamia

回答

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 = @" ";

+0

这适用于标签,我以前用它来标签,但对UITextViews不可靠。 – Quentamia

+0

对不起,我还没有试过UITextViews。它造成了什么问题? –

-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无效。

+0

它没有任何意义。 – Gabox

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; 
    } 
} 
1

IOS 7不支持该属性contentSize了。尝试使用这个

CGFloat textViewContentHeight = textView.contentSize.height; 
textViewContentHeight = ceilf([textView sizeThatFits:textView.frame.size].height + 9); 

这解决了我的问题。

相关问题