2014-11-06 38 views
0

我使用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; 

帮助需要

+0

也许你错过了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

+0

我应该然后使用什么编码因为html不会是网页,但主要用于格式化文本 – 2014-11-06 06:59:02

+0

是否替换'@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDoc umentType};'带'@ {NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)};'help? – Zhang 2014-11-06 07:00:46

回答

0

我想和这个工作

NSString* view = @"<i>Testing</i> and this is <b>bold</b> <br> This should be in next line"; 
NSAttributedString *preview =[[NSAttributedString alloc] initWithData:[view dataUsingEncoding:NSUTF8StringEncoding] 
           options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
              NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
         documentAttributes:nil error:nil]; 

UILabel *lbl = [[UILabel alloc] initWithFrame:cell.textLabel.frame]; 
[lbl setAttributedText:preview]; 
0

正如在评论中提到的,对我来说这个问题是不是与HTML渲染但忘记设置“线路数量“在UILabel上为0。

0

我有一些关于此问题的问题,并获取ASCII代码,而不是像'&'和一些其他代码一些特殊字符。所有的问题,只要使用:

NSString *html = [command.arguments objectAtIndex: 
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding]; 
NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": data, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}}; 
data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; 
NSAttributedString *decodedString = [[NSAttributedString alloc] initWithData:data 
                     options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} 
                    documentAttributes:NULL 
                       error:NULL];`