我正在开发一个简单的RSS阅读器应用程序作为Xcode中的初学者项目.我目前设置它解析feed,并放置标题,发布日期,描述和内容,并将其显示在WebView中.
我最近决定在用于选择帖子的TableView中显示描述(或内容的截断版本).但是,这样做时:
cell.textLabel?.text = item.title?.uppercaseString
cell.detailTextLabel?.text = item.itemDescription //.itemDescription is a String
Run Code Online (Sandbox Code Playgroud)
它显示了帖子的原始HTML.
我想知道如何将HTML转换为纯文本,仅用于TableView的详细UILabel.
谢谢!
我UITextView在其中使用我显示NSAttributedString.我从服务器获得HTML字符串.我使用下面的代码将HTML转换为NSAttributedString.
NSMutableAttributedString *attrib = [[NSMutableAttributedString alloc]initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)
要应用字体,我尝试了以下2个代码.
要应用属性我使用下面的代码.
[attrib addAttribute:NSFontAttributeName
value:myFont
range:NSMakeRange(0, attrib.length)];
Run Code Online (Sandbox Code Playgroud)
对于自定义字符串首先我在下面创建NSString,然后隐藏NSAttributedString
NSString *strHTML = [NSString stringWithFormat:@"<span style=\"font-family: myFont; font-size: 12\">%@</span>",@"my string"];
Run Code Online (Sandbox Code Playgroud)
使用两个代码字体已成功更改.但Bold和Italic未应用仅Underline应用于NSAttributedString.
我参考下面的链接.
对于链接3,我应该从服务器端应用字体和标签,然后检索HTML字符串?
任何帮助都会得到满足!