iOS UILabel顯示html標簽


iOS7以后系統提供了顯示html標簽的方法

UIKIT_EXTERN NSString *const NSHTMLTextDocumentType NS_AVAILABLE_IOS(7_0);

直接上代碼

NSString *str = @"<font color=\"#6c6c6c\">滿20減5 滿40減15,還剩<font color=\"#ff9147\">113天";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 50, 300, 50)];
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
label.attributedText = attrStr;
//如果想要改變文字的字體,請在設置attributedText之后設置
label.font = [UIFont systemFontOfSize:20];
[self.view addSubview:label];

代碼效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM