【发布时间】:2014-12-12 10:35:12
【问题描述】:
这是我的 HTML 响应
<p><strong>Ezekiel 33:1-20 <br /><br /></strong>
<strong><sup>1</sup></strong> The word of the Lord came to me: <strong><sup>2 </sup></strong>
“Son of man, speak to your people and tell them: Suppose I bring the sword against a land, and the people of that land select a man from among them, appointing him as their watchman,
<strong><sup>3 </sup></strong>
我想在 UITextView 中将上述 HTML 显示为属性字符串,条件为 <strong> 到 </strong> 为红色,这是我的代码。
attributedString2 = [[NSMutableAttributedString alloc] initWithData:[[NSString stringWithFormat:@"%@",readingPartCntnt]
dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes:nil error:nil];
NSRange rang = [readingPartCntnt rangeOfString:@"<strong>" options:NSBackwardsSearch];
if (rang.location == NSNotFound) {
NSLog(@"string was not found");
} else {
NSLog(@"position %lu", (unsigned long)rang.location);
}
NSRange rang1 = [readingPartCntnt rangeOfString:@"</strong>" options:NSBackwardsSearch];
if (rang1.location == NSNotFound) {
NSLog(@"string was not found");
} else {
NSLog(@"position %lu", (unsigned long)rang1.location);
}
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rang];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:rang1];
它没有正确应用。请帮我解决。
【问题讨论】:
-
我建议用原始字符串中的红色标记替换强标记,使用 NSAttritedString 方法读取它。
标签: ios8 uitextview nsattributedstring