現在の回答は問題ありません。+1 しました。しかし、それはヒントに過ぎず、本当の解決策ではありませんでした。OPの質問に対する解決策を探している場合は、こちらをご覧ください。
次の点に注意する必要があります。
最初にこれらのメソッドを実装します。
- (NSString *)styledHTMLwithHTML:(NSString *)HTML {
NSString *style = @"<meta charset=\"UTF-8\"><style> body { font-family: 'HelveticaNeue'; font-size: 20px; } b {font-family: 'MarkerFelt-Wide'; }</style>";
return [NSString stringWithFormat:@"%@%@", style, HTML];
}
- (NSAttributedString *)attributedStringWithHTML:(NSString *)HTML {
NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType };
return [[NSAttributedString alloc] initWithData:[HTML dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL error:NULL];
}
後でそのように使用します:
// This is a string that you might find in your model
NSString *html = @"This is <b>bold</b>";
// Apply some inline CSS
NSString *styledHtml = [self styledHTMLwithHTML:html];
// Generate an attributed string from the HTML
NSAttributedString *attributedText = [self attributedStringWithHTML:styledHtml];
// Set the attributedText property of the UILabel
label.attributedText = attributedText;