iOS,UITextView中显示html文本

iOS,UITextView中显示html文本,第1张

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 30, 20, 114)]

textView.backgroundColor = [UIColor redColor]

[self.view addSubview:textView]

NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p>![]( https://img-blog.csdnimg.cn/20190927151043371.png )"

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil]

textView.attributedText = attributedString

UITextView*textView = [[UITextViewalloc]initWithFrame:CGRectMake(20,30,100,199)]

textView.backgroundColor = [UIColor redColor]

[self.view addSubview:textView]

NSString *htmlString = @"<h1>header</h1><h2>Subheader</h2><p>Some<em>text</em></p>![](http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg)"

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil]

textView.attributedText = attributedString

- (void)viewDidLoad

{

    [super viewDidLoad]

    

    NSString *strHTML = @"<p>你好</p><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp这是一个例子,请显示</p><p>外加一个table</p><table><tbody><tr class=\"firstRow\"><td valign=\"top\" width=\"261\">aaaa</td><td valign=\"top\" width=\"261\">bbbb</td><td valign=\"top\" width=\"261\">cccc</td></tr></tbody></table><p><br/></p>"

    

    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]

    [self.view addSubview:webView]

    

    [webView loadHTMLString:strHTML baseURL:nil]

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7273099.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存