mil*_*ari 77 iphone uitextview ios
如何在textview中显示HTML文本?
例如,
string <h1>Krupal testing <span style="font-weight:
bold;">Customer WYWO</span></h1>
Run Code Online (Sandbox Code Playgroud)
假设文本是粗体,因此它在textview中显示为粗体字符串,但我想显示普通文本.这可以在iPhone SDK中使用吗?
Bho*_*opi 229
使用以下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;
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 52
在iOS 5-上使用UIWebView.
在iOS 6+上您可以使用UITextView.attributedString,请参阅/sf/answers/1469725981/了解具体方法.
还有一种无证 -[UITextView setContentToHTMLString:]方法.如果要提交到AppStore,请不要使用此选项.
pab*_*ros 39
对于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
Run Code Online (Sandbox Code Playgroud)
对于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
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 17
BHUPI的
答案是正确的,但如果你想将UILabel或UITextView的自定义字体与HTML内容结合起来,你需要纠正你的html:
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;
Run Code Online (Sandbox Code Playgroud)
sEl*_*yan 12
您可以查看OHAttributedLabel类,我使用它们来克服textField的这种问题.在此,他们重写了drawRect方法以获得所需的样式.
https://github.com/AliSoftware/OHAttributedLabel
我的第一个回应是在iOS 7引入显式支持在常用控件中显示属性字符串之前做出的.现在,您可以设置attributedText的UITextView一个NSAttributedString使用HTML创建内容:
-(id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
Run Code Online (Sandbox Code Playgroud)
- initWithData:options:documentAttributes:error:(Apple Doc)
原始答案,保留历史:
除非您使用a UIWebView,否则您的解决方案将直接依赖CoreText.正如ElanthiraiyanS所指出的,一些开源项目已经出现,以简化富文本呈现.我建议使用NSAttributedString-Additions-For-HTML(编辑:该项目已被取代DTCoreText),其中包含用于生成和显示HTML中的属性字符串的类.
| 归档时间: |
|
| 查看次数: |
116043 次 |
| 最近记录: |