如何在 UITextView 中加载 HTML? [英] How to load HTML in UITextView?

查看:49
本文介绍了如何在 UITextView 中加载 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 achor 标签的 HTML 内容.我只想在 UITextView 中加载 HTML 内容.

I have a HTML contents with achor tag in it. I just want to load that HTML content in UITextView.

推荐答案

您可以像这样将 HTML 文档转换为 NSAttributedString:

You can convert an HTML document to NSAttributedString like so:

// The HTML needs to be in the form of an NSString
NSError *error = nil;
NSAttributedString *attString = [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:&error];
if (error) {
    NSLog(@"Error: %@ %s %i", error.localizedDescription, __func__, __LINE__);
} else {
    // Clear text view
    textView.text = @"";
    // Append the attributed string
    [textView.textStorage appendAttributedString:attString];
} 

如果您遇到任何问题,请尝试在所有地方将编码更改为 NSASCIIStringEncoding.

If you come across any problems then try changing the encoding to NSASCIIStringEncoding in all places.

这篇关于如何在 UITextView 中加载 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆