iOS UILabel顯示html標籤

iOS7之後系統提供了顯示html標籤的方法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];

代碼效果spa

 

相關文章
相關標籤/搜索