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>";
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes: nil error: nil];
textView.attributedText = attributedString;
本文介绍如何将HTML格式的字符串转换为NSAttributedString,并将其显示在UITextView中。通过使用NSData和NSAttributedString的初始化方法,可以解析HTML内容并应用到文本视图上,实现富文本效果。
3172

被折叠的 条评论
为什么被折叠?



