簡體   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