我从服务器&获取html代码我想在UITextView中显示它。我需要解析html&在UITextView中显示纯文本。我使用下面的代码来解析来自此post
的html现在我需要将li
标记替换为实际数字。我怎样才能做到这一点 ?
例如输入
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
输出应为
答案 0 :(得分:1)
将您的功能替换为以下内容并传递您已发布的HTML字符串片段。
NSString *str = @"<ol>"
"<li>Coffee</li>"
"<li>Tea</li>"
"<li>Milk</li>"
"</ol>";
[self stringByStrippingHTML:str];
替换字符串和生成欲望输出的功能
- (NSString *)stringByStrippingHTML:(NSString *)inputString
{
NSMutableString *outString;
if (inputString)
{
outString = [[NSMutableString alloc] initWithString:inputString];
if ([inputString length] > 0)
{
NSRange r;
int index = 1;
while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
if([[outString substringWithRange:r] isEqualToString:@"<li>"])
{
outString = [[outString stringByReplacingCharactersInRange:r withString:[NSString stringWithFormat:@"\n %d. ",index++]] mutableCopy];
}
else
[outString deleteCharactersInRange:r];
}
}
}
return outString;
}