Wednesday, 9 April 2014

Calculating Height of UITextView in iOS7



Many times in application we need to calculate height of UITextView to make it grow and shrink according to the text contained within it.

Now many of you will say calculating height of UITextView is not a big deal.

Yes its true for iOS6 and below version. But not true from iOS7 and higher.

In iOS6 we can get height of a UITextView simply using the contentSize.

eg.

  [myTextView setText:@"here goes my text"];

  CGFloat heightOfTextView = myTextView.contentSize.height;


In iOS7 may be it's a bug or due to deprecation, contentSize does not provide you the accurate height of the UITextView.

So below is the code snippet i used in my application for getting the height of UITextView:

Just set text NSString to UITextView and call this method sending UITextView as parameter


  [myTextView setText:@"here goes my text"];

  [self measureHeightOfUITextView: myTextView];


- (CGFloat)measureHeightOfUITextView:@"here goes my text"];​(UITextView *)textView

{

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)

    {

        // This is the code for iOS 7. contentSize no longer returns the correct value, so

        // we have to calculate it.

        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;

    }

}



I got help from below links for above code snippet:
Link 1
Link 2

No comments:

Post a Comment