如何将Hex HTML转换为NSString

时间:2015-01-20 09:50:10

标签: ios iphone

我有:

NSString = @"%3Cfont+color%3D%22%23660000%22%3EBeta1%3C%2Ffont%3E++%3Cfont+color%3D%22%23000000%22%3Ebeta3%3C%2Ffont%3E"

如何将此Hex HTML字符串转换为NSSting或HTML字符串,以便我可以使用其字体和颜色属性以及获取文本?

1 个答案:

答案 0 :(得分:0)

字符串看起来乍一看是编码的,所以:

NSString *input = @"%3Cfont+color%3D%22%23660000%22%3EBeta1%3C%2Ffont%3E++%3Cfont+color%3D%22%23000000%22%3Ebeta3%3C%2Ffont%3E";
NSString *result = [input stringByReplacingOccurrencesOfString:@"+" withString:@" "];
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"%@", result);       

然后将html解析为NSAttributedString

NSAttributedString *as = [NSAttributedString attributedStringWithHTML:result];

注意iOS不支持开箱即用(osx确实;))

(在iOS上使用DTCoreText库进行HTML解析)