ios - 使用 uitextview 在 ios 版本 6 和 7 中显示 html 文本

标签 ios ios7 ios6 uitextview

我正在使用网络服务在我的 iOS 应用程序中显示内容。

在网络上,数据是使用丰富的编辑文本框存储的。

因此作为响应,我正在获取要显示的 HTML 文本。

现在我已经使用 UITextView 在我的应用程序中显示 html 文本。

[_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;

现在我需要计算 uitextview 的大小,以便我可以重新定位 UItextview 下面的 View 。

为了计算 uitextview 的高度,我使用了下面描述的函数。

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

现在它正在正确返回高度。

但现在的问题是,当它在 ios 7.0 以下版本中显示文本时,其文本大小是根据以编程方式给定的,而在 ios 7 中,它不显示给定字体大小的文本。

那么在ios 7中显示fontsize的解决方案是什么。

提前致谢 斯卡尼亚

最佳答案

很简单。我希望这对正在为 Uitextview 动态高度问题苦苦挣扎的人有所帮助。

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

谢谢 斯卡尼亚。

关于ios - 使用 uitextview 在 ios 版本 6 和 7 中显示 html 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992718/

相关文章:

ios6 - MKMapView NSInvalidArgumentException ios6 中的无效区域崩溃

ios - 如何为 NSURLConnection 定义 NSMutableData 实例?

iphone - UILocalNotification - 如何发现用户操作是否来自通知中心?

ios - NSFileHandle fileHandleForWritingAtPath 返回 nil 和有效路径

ios7 - 检测应用后台iOS7中的抖动事件

iphone - 如何为 iOS 应用程序创建自定义错误域?

播放视频后 iOS 7 状态栏消失

ios - 上传到s3,iOS成功回调

ios - 快速返回另一页后, TableView 单元格正在复制

ios - 如何为动画添加完成?