Ios8 字符串替换-属性化字符串

Ios8 字符串替换-属性化字符串,ios8,uitextview,nsattributedstring,Ios8,Uitextview,Nsattributedstring,这是我的HTML回复 <p><strong>Ezekiel 33:1-20&nbsp;<br /><br /></strong> <strong><sup>1</sup></strong> The word of the&nbsp;Lord&nbsp;came to me:&nbsp;<strong><sup>2&nbsp;&

这是我的HTML回复

<p><strong>Ezekiel 33:1-20&nbsp;<br /><br /></strong>
<strong><sup>1</sup></strong> The word of the&nbsp;Lord&nbsp;came to me:&nbsp;<strong><sup>2&nbsp;</sup></strong>
&ldquo;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,&nbsp;
<strong><sup>3&nbsp;</sup></strong>
以西结书33:1-20 耶和华的话临到我说∶『2』 &ldquo;人子阿,你晓谕你的百姓,告诉他们:我若使刀剑临到一地,那地的百姓就从他们中间拣选一个人,立他作守望的人, 3 我想在UITextView中将上述HTMLs显示为属性字符串,条件为红色,这里是我的代码

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];
attributedString2=[[NSMutableAttributedStringAlloc]initWithData:[[NSString stringWithFormat:@“%@”,readingPartCntnt]
dataUsingEncoding:NSInocDestringEncoding]选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
文档属性:无错误:无];
nsrrange rang=[ReadingPartCnt rangeOfString:@“”选项:NSBackardsSearch];
if(rang.location==NSNotFound){
NSLog(@“未找到字符串”);
}否则{
NSLog(@“位置%lu”,(无符号长)范围。位置);
}
NSRange rang1=[ReadingPartCnt rangeOfString:@“”选项:NSBackardsSearch];
if(rang1.location==NSNotFound){
NSLog(@“未找到字符串”);
}否则{
NSLog(@“位置%lu”,(无符号长)rang1.location);
}
[attributedString2 addAttribute:NSForegroundColorAttributeName值:[UIColor redColor]范围:范围];
[attributedString2 addAttribute:NSForegroundColorAttributeName值:[UIColor redColor]范围:rang1];

未正确应用。请帮助我解决此问题。

以下代码为我解决了此问题:

readingPartCntnt = [readingPartCntnt stringByReplacingOccurrencesOfString:@"<strong>" withString:@"<strong style=\"color: red;\">"];

attributedString2 = [[NSMutableAttributedString alloc] initWithData:[[NSString stringWithFormat:@"%@",readingPartCntnt] 
    dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } 
    documentAttributes:nil error:nil];

self.textView.attributedText = attributedString2.copy;
readingPartCntnt=[readingPartCntnt StringByReplacingOfString:@“”与字符串:@“”;
attributedString2=[[NSMutableAttributedStringAlloc]initWithData:[[NSString stringWithFormat:@“%@”,readingPartCntnt]
dataUsingEncoding:NSInocDestringEncoding]选项:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
文档属性:无错误:无];
self.textView.attributedText=attributedString2.copy;

这将在创建属性字符串之前更改原始字符串的CSS样式。

我建议使用原始字符串中的红色标记替换强标记,使用NSAttritedString方法读取它。