In any case, to reliably calculate the height of a UITextView?

I tried sizeWithFont:constrainedToSize:lineBreakModeand sizeToFitboth of which did not return the correct result.

It is assumed that the property contentSizereturns the correct height, but it does not work until the text representation is displayed on the screen, and I need to calculate the height before the text is visible (it determines the height a UITableViewCell.

Has anyone found any other method, a custom text view or the like, that correctly calculates the height of a UITextView?

EDIT I must clarify that I want the ideal height based on the content of the text view.

+5
source share
4 answers

IOS 7 contentSize.

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

.

+1

, :

+ (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

, 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;
    }
}
0

, 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;
...

This is from the accepted answer here , see notes. My attempt at explanation is given here if this is not valid for UITextviews that are added to UITableViewCells instead of UIScrollViews (for some reason).

-2
source

All Articles