【发布时间】:2018-07-15 01:55:57
【问题描述】:
在我的应用程序中,我必须更改来自 JSON 响应的 string 部分的字体
> "<span class=\"drop_data_user\">Andrew James</span> liked your comment
> \"hiiiiiiiiiiiiiii\" that you posted."
要将其转换为属性字符串,我使用以下代码
NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[NotificationTxt dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
我要更改发件人的字体颜色
【问题讨论】:
-
您如何定义“Andrew James”是发件人?它总是在那个特定的 span html 标记之间吗?
-
是的,它总是在 span 标签之间
-
由于该标签不会被“解释”而是被提交(在 Playground 上测试),我建议使用
rangeOfString:、NSRegularExpression找到它(如果它始终是同一个) ,NSScanner等,然后自己添加(或替换)一个 HTML 标记,为其定义颜色。 -
你试过我的建议了吗?
标签: ios objective-c nsstring nsattributedstring