我正在获得HTML格式的标题
<p> <span style ="color:#000000;"> <strong>示例</ strong> </ span> </ p>
我需要在UILabel中显示这个HTML字符串.颜色代码和字体大小应与HTML中的相同.当我将HTML字符串转换为NSString时,只有文本"示例"即将到来,而不是颜色.
有什么解决方案吗?
Thnx提前
直到现在我尝试通过以下方式使用NSAttributedString,但通过这种方式整个HTML正在打印:
UIFont *font = [UIFont systemFontOfSize:14.0];
UIFont *secondFont = [UIFont systemFontOfSize:10.0];
NSMutableDictionary *firstAttributes;
NSMutableDictionary *secondAttributes;
NSDictionary *firstAttributeFont = @{NSFontAttributeName:font};
NSDictionary *secondAttributeFont = @{NSFontAttributeName:secondFont};
[firstAttributes addEntriesFromDictionary:firstAttributeFont];
[secondAttributes addEntriesFromDictionary:secondAttributeFont];
[firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];
[secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];
NSString* completeString = [NSString stringWithFormat:@"%@",strTitle];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:strTitle]];
// [attributedString setAttributes:secondAttributes range:[completeString rangeOfString:self.secondAttributeText]];
Cell.lbl_Title.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
san*_*ana 126
对于iOS7或更高版本,您可以使用:
NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr =
[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;
Run Code Online (Sandbox Code Playgroud)
Bas*_*Zen 28
斯威夫特2
let htmlText = "<p>etc</p>"
if let htmlData = htmlText.dataUsingEncoding(NSUnicodeStringEncoding) {
do {
someLabel.attributedText = try NSAttributedString(data: htmlData,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
} catch let e as NSError {
print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let htmlText = "<p>etc</p>"
if let htmlData = htmlText.data(using: String.Encoding.unicode) {
do {
let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
} catch let e as NSError {
print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
}
}
Run Code Online (Sandbox Code Playgroud)
Jak*_*lář 18
对于Swift 4及以上版本的使用:
guard let data = "foo".data(using: String.Encoding.unicode) else { return }
try? titleLabel.attributedText =
NSAttributedString(data: data,
options: [.documentType:NSAttributedString.DocumentType.html],
documentAttributes: nil)
Run Code Online (Sandbox Code Playgroud)
我在UITextView上执行了以下操作:
[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];
Run Code Online (Sandbox Code Playgroud)
或者您可以使用RTLabel库:https: //github.com/honcheng/RTLabel在标签上显示html文本及其格式.
extension UILabel {
func set(html: String) {
if let htmlData = html.data(using: .unicode) {
do {
self.attributedText = try NSAttributedString(data: htmlData,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
} catch let e as NSError {
print("Couldn't parse \(html): \(e.localizedDescription)")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63056 次 |
| 最近记录: |