今天做項目的時候需要將帶html標籤顯示在label中,以下是實現這個功能的相關代碼:
主要是設置label的attributedText屬性即可。
UIFont *font = [UIFont systemFontOfSize:14.0f];
NSString * htmlString = [NSString stringWithFormat:@"%@",mind.mainContent];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
cell.lblMind.attributedText =attrStr;
//cell.lblMind.numberOfLines = 0;
cell.lblMind.font = font;
注:關於改變字體問題,雖然在xib文件中將label的字體設置,但是運行的時候發現字體大小沒有改變,如果想要改變字體的大小,必須使用代碼重新設置。
以上就是ios中,希望對大家有幫助。