html - 检查 NSString 是否是 html 字符串?

标签 html ios objective-c nsattributedstring nsmutableattributedstring

我正在创建一个应显示字符串列表的应用程序,该字符串是从服务器返回的,可以是 html 也可以不是。 我目前正在 UILabel 中设置文本。为此,我正在使用以下内容

    NSMutableAttributedString *attributedTitleString = [[NSMutableAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
cell.label.attributedText =attributedTitleString;

当文本是 html 时,一切都完美无缺,因为字体和对齐方式在 html 中返回。当文本是普通文本时会出现此问题。字体、文本对齐方式、文本大小和其他不再受到尊重。

那么如何检查文本是否为 html 字符串呢? 对于普通文本,我将使用以下内容:

cell.label.text =title;

我已经尝试在论坛上搜索,但仍然没有得到我的问题的任何答案。

最佳答案

这工作正常,您需要输入:

单元格标签。 attributedText = title; 也包括普通文本。

因为它工作正常。运行以下代码。

//如果是HTML文本

NSString *htmlstr = `@"This is <font color='red'>simple</font>"`;

NSMutableAttributedString *attributedTitleString = [[NSMutableAttributedString alloc] initWithData:[htmlstr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

textField.attributedText =attributedTitleString;

textField.font = [UIFont fontWithName:@"vardana" size:20.0];

//如果是普通文本。

NSString *normalStr = @"This is Renuka";

NSMutableAttributedString *NorAttributedTitleString = [[NSMutableAttributedString alloc] initWithData:[normalStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

textField.attributedText = NorAttributedTitleString;

textField.font = [UIFont fontWithName:@"vardana" size:20.0];

关于html - 检查 NSString 是否是 html 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35913370/

相关文章:

objective-c - 禁用 NSDocument 的 "Revert To"和 "Duplicate"菜单项

objective-c - NSTreeController 添加 :method not adding "after the current selection"

html - 如何让div跳一行

ios - 在打开的相机上呈现相机胶卷 UIImagePickerController

html - 如何将多个 div 排在一行且间距均匀

ios - 在 Xamarin iOS 13 中自定义导航栏不起作用

ios - 计算类中的行数并选择给定数字的行

ios - 在 iOS 中一起加载所有表格 View 单元格图像

html - 一栏中的图片导致文本在其旁边的栏中被下推

html - 为什么 Firefox 和 Chrome 只是决定不呈现我的页面的一部分?