NSAttributedString不呈现html换行符

时间:2014-11-06 06:50:24

标签: ios objective-c uilabel nsattributedstring

我使用attributedText的{​​{1}}属性来渲染html。

渲染粗体和斜体的工作正常。但是,当我尝试渲染html换行符时,它会切断换行符后出现的所有文本。

label

需要帮助

3 个答案:

答案 0 :(得分:2)

我遇到了一些与此相关的问题并获取了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];`

答案 1 :(得分:1)

我试过了,这很有用

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];

答案 2 :(得分:1)

正如评论中所提到的,对我来说,这个问题不是HTML渲染,而是忘记设置&#34;行数&#34;在UILabel上为0。