如何在textview中显示HTML文本?
例如,
string <h1>Krupal testing <span style="font-weight:
bold;">Customer WYWO</span></h1>
假设文本是粗体,因此它在textview中显示为粗体字符串 但我想显示普通文字。这可以在iPhone SDK中使用吗?
答案 0 :(得分:228)
使用以下ios 7+代码块。
NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg' width=70 height=100 />";
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;
答案 1 :(得分:51)
在iOS 5-上使用UIWebView。
在iOS 6+上,您可以使用UITextView.attributedString
,请参阅https://stackoverflow.com/a/20996085了解具体方法。
还有一个未记录的 -[UITextView setContentToHTMLString:]
方法。如果要提交到AppStore,请不要使用此选项。
答案 2 :(得分:36)
Swift 4 和 Swift 4.2:
let htmlString = "<html>" +
"<head>" +
"<style>" +
"body {" +
"background-color: rgb(230, 230, 230);" +
"font-family: 'Arial';" +
"text-decoration:none;" +
"}" +
"</style>" +
"</head>" +
"<body>" +
"<h1>A title</h1>" +
"<p>A paragraph</p>" +
"<b>bold text</b>" +
"</body></html>"
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil)
textView.attributedText = attributedString
Swift 3 :
let htmlString = "<html>" +
"<head>" +
"<style>" +
"body {" +
"background-color: rgb(230, 230, 230);" +
"font-family: 'Arial';" +
"text-decoration:none;" +
"}" +
"</style>" +
"</head>" +
"<body>" +
"<h1>A title</h1>" +
"<p>A paragraph</p>" +
"<b>bold text</b>" +
"</body></html>"
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedString
答案 3 :(得分:15)
答案是正确的,但是如果你想将UILabel或UITextView的自定义字体与HTML内容结合起来,你需要更正你的html:BHUPI的
NSString *htmlString = @"<b>Bold</b><br><i>Italic</i><p> <del>Deleted</del><p>List<ul><li>Coffee</li><li type='square'>Tea</li></ul><br><a href='URL'>Link </a>";
htmlString = [htmlString stringByAppendingString:@"<style>body{font-family:'YOUR_FONT_HERE'; font-size:'SIZE';}</style>"];
/*Example:
htmlString = [htmlString stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}</style>",_myLabel.font.fontName,_myLabel.font.pointSize]];
*/
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;
答案 4 :(得分:12)
你可以查看OHAttributedLabel类,我使用它们来克服textField的这种问题。在这里,他们重写了drawRect方法以获得所需的样式。
答案 5 :(得分:9)
我的第一个回应是在iOS 7引入显式支持在常用控件中显示属性字符串之前做出的。您现在可以使用以下内容将attributedText
UITextView
设置为从HTML内容创建的NSAttributedString
:
-(id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
- initWithData:options:documentAttributes:error: (Apple Doc)
原始答案,保留历史记录:
除非您使用UIWebView
,否则您的解决方案将直接依赖CoreText
。正如ElanthiraiyanS所指出的,一些开源项目已经出现,以简化富文本呈现。我建议 NSAttributedString-Additions-For-HTML (编辑:项目已被取代DTCoreText),其中包含用于生成和显示HTML中的属性字符串的类。< / p>
答案 6 :(得分:4)
来自BHUPI的答案适合我。
代码转移到swift,如下所示:
注意“allowLossyConversion:false”
如果将值设置为true,则会显示纯文本。
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString
答案 7 :(得分:3)
对于Swift3
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString
答案 8 :(得分:0)
对于某些情况,UIWebView是一个很好的解决方案。这是因为:
如果html很复杂或包含表格(so example)
,使用NSAttributedString会导致崩溃要将文本加载到Web视图,您可以使用以下代码段(仅示例):
func loadHTMLText(_ text: String?, font: UIFont) {
let fontSize = font.pointSize * UIScreen.screens[0].scale
let html = """
<html><body><span style=\"font-family: \(font.fontName); font-size: \(fontSize)\; color: #112233">\(text ?? "")</span></body></html>
"""
self.loadHTMLString(html, baseURL: nil)
}
答案 9 :(得分:-2)
您还可以使用其他方法。 Three20库提供了一种方法,通过它我们可以构造一个样式化的textView。您可以在此处获取图书馆:http://github.com/facebook/three20/
类TTStyledTextLabel有一个名为textFromXHTML的方法:我想这可以达到目的。但是在readonly模式下它是可能的。我不认为它会允许编写或编辑HTML内容。
还有一个问题可以帮助您:HTML String content for UILabel and TextView
我希望它有所帮助。
答案 10 :(得分:-3)
NSDoc将字符串中的文本文件保存为html文件,然后同时将其加载到与UITextView位于同一位置的webview中。