繁体   English   中英

如何使用UILabel显示包含背景颜色和边框半径的HTML字符串?

[英]How to use UILabel to show the HTML string that contains the background-color and the border-radius?

NSString *html = [[NSString alloc] initWithString:@"<div style='background-color:#34c083;line-height:20px;width:65px;border-radius:5px;color:#fff;margin-top:5px;;padding-top:2px;height:20px;'><a href='https://www.baidu.com' style='text-decoration:none;color:#fff;'>hello!!!</a></div>"];       
NSError* error = nil;

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType} documentAttributes:nil error:&error];

UILabel *textView = [UILabel new];
textView.attributedText = text;

结果与我预期的不同。 为什么?如何解决?谢谢。

试试下面的代码

NSString *html = @"<div style='background-color:#34c083;line-height:20px;width:65px;border-radius:5px;color:#fff;margin-top:5px;;padding-top:2px;height:20px;'><a href='https://www.baidu.com' style='text-decoration:none;color:#fff;'>hello!!!</a></div>";
NSError* error = nil;

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType} documentAttributes:nil error:&error];

UILabel *textView = [UILabel new];
textView.attributedText = text;
[textView.layer setCornerRadius:5.0];
[textView.layer setMasksToBounds:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM