我使用attributedText
屬性label
來呈現html。NSAttributedString不呈現html換行符
它與渲染粗體和斜體正常工作。但是,當我嘗試呈現HTML換行符時,它只會切斷換行符後出現的所有文本。
NSString* view = @"<i>Testing</i> and this is <b>bold</b> <br> This should be in next line";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *preview = [[NSAttributedString alloc] initWithData:[view dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];
self.labelView.attributedText = preview;
幫助需要
也許你錯過了NSUTF8StringEncoding字符編碼部分解決,如下所示:http://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in -ios'[[NSAttributedString的alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 選項:@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:無誤差:無];' – Zhang 2014-11-06 06:57:37
我應該然後使用什麼編碼因爲html不會是網頁,但主要用於格式化文本 – 2014-11-06 06:59:02
是否替換'@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDoc umentType};'帶'@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)};'help? – Zhang 2014-11-06 07:00:46