0
votes

I am using webservice to display content in my ios application.

On web the data is stored using rich edit text box.

so in response I am getting HTML text to display.

Now I had used UITextView to display html text in my application.

[_txt_desc setValue:@"html text here" forKey:@"contentToHTMLString"];
    [_txt_desc setFont:[UIFont fontWithName:custom_font_name size:is_ipad?18:14]];
    _txt_desc.editable = NO;
    _txt_desc.scrollEnabled = NO;

Now I need to calculate size of the uitextview so I can reposition the views below the UItextview.

For calculating height of the uitextview I had used function described below.

+ (CGFloat)measureHeightOfUITextView:(UITextView *)textView withHtmlString:(NSString *)htmlString
{
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.
    //
    // 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)
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
    CGFloat measuredHeight=0;
    if ([htmlString length]>0) {
        textView.translatesAutoresizingMaskIntoConstraints = NO;
//            NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:   [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSParagraphStyleAttributeName : paragraphStyle,NSFontAttributeName:textView.font} documentAttributes:nil error:nil];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSFontAttributeName:[UIFont fontWithName:custom_font_name size:is_ipad?16:14]} documentAttributes:nil error:nil];

        textView.attributedText = attributedString;

        // Take account of the padding added around the text.
    }
    CGRect frame = textView.bounds;
    NSLog(@"content size %@",NSStringFromCGSize(textView.frame.size));
    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.


    NSDictionary *attributes = @{ NSFontAttributeName: textView.font, NSParagraphStyleAttributeName : paragraphStyle,NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };

    CGRect size = [textToMeasure boundingRectWithSize:CGSizeMake(CGRectGetWidth(frame), MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];

    measuredHeight = ceilf(CGRectGetHeight(size) + topBottomPadding);


    return measuredHeight;
}
else
{
    CGSize constraintSize = CGSizeMake(textView.frame.size.width, MAXFLOAT);
    CGSize labelSize = [textView.text sizeWithFont:textView.font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    int occurance = 0;
    occurance=(int)[[htmlString componentsSeparatedByString:@"<p>"] count];
    float height=0;
    if(occurance>1){
        height =occurance*(is_ipad?20:10);
    }
    occurance = (int)[[htmlString componentsSeparatedByString:@"<br/>"] count];
    if(occurance>1){
        height +=occurance*(is_ipad?20:10);
    }
    occurance = (int)[[htmlString componentsSeparatedByString:@"<br>"] count];
    if(occurance>1){
        height +=occurance*(is_ipad?20:10);
    }
    occurance = (int)[[htmlString componentsSeparatedByString:@"<h2>"] count];
    if(occurance>1){
        height +=occurance*(is_ipad?30:20);
    }
    occurance = (int)[[htmlString componentsSeparatedByString:@"<h3>"] count];
    if(occurance>1){
        height +=occurance*(is_ipad?30:20);
    }

    return labelSize.height+(is_ipad?30:20)+textView.font.pointSize+height;
}
}

Now it is returning the height properly.

But now issue is when it is displaying text in below version of ios 7.0 its text size is according to given programmatically while in ios 7 its not displaying text as given font size.

So what can be the solution for fontsize to display in ios 7.

Thanks in advance bskania

1

1 Answers

2
votes

Its simple. I hope this will help some one who is struggling with the Uitextview dynamic height issue.

[_txt_desc setValue:@"html text here" forKey:@"contentToHTMLString"];
[_txt_desc sizeThatFits:CGSizeMake(_txt_desc.frame.size.width,FLT_MAX)].height;

thanks bskania.