我需要在UILabel中显示下标和上标(仅阿拉伯数字)。数据取自XML文件。这是XML文件的片段:
<text><![CDATA[Hello World X\u00B2 World Hello]]></text>
它应该显示X2(2作为上标)。当我从NSXMLParser中读取字符串并将其显示在UILabel中时,它将显示为X \ u00B2。关于如何使其运作的任何想法?
最佳答案
我认为您可以执行以下操作,假设已将CDATA内容读入NSString并传递给此函数:
-(NSString *)removeUnicodeEscapes:(NSString *)stringWithUnicodeEscapes {
unichar codeValue;
NSMutableString *result = [stringWithUnicodeEscapes mutableCopy];
NSRange unicodeLocation = [result rangeOfString:@"\\u"];
while (unicodeLocation.location != NSNotFound) {
// Get the 4-character hex code
NSRange charCodeRange = NSMakeRange(unicodeLocation.location + 2, 4);
NSString *charCode = [result substringWithRange:charCodeRange];
[[NSScanner scannerWithString:charCode] scanHexInt:&codeValue];
// Convert it to an NSString and replace in original string
NSString *unicodeChar = [NSString stringWithFormat:%C", codeValue];
NSRange replacementRange = NSMakeRange(unicodeLocation.location, 6);
[result replaceCharactersInRange:replacementRange withString:unicodeChar];
unicodeLocation = [result rangeOfString:@"\\u"];
}
return result;
}
我还没有机会尝试一下,但是我认为基本方法会奏效
关于iphone - xml文件的CDATA中的下标和上标。使用UILabel显示已解析的XML内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542159/