iphone - xml文件的CDATA中的下标和上标。使用UILabel显示已解析的XML内容

标签 iphone ios xml uilabel cdata

我需要在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/

相关文章:

iphone - 无法在 ios 6 中创建存档文件

iphone - 如何使用 iPhone 相机检查光线强度

ios - 由于 'private' 级保护 - swift,无法访问“restoreProgress”

c - 发送带有 XML 内容的 POST http 请求

iphone - 我可以支持 iOS 3.0 到 6.0 版本吗?

iphone - 在输出中使用 '%'

iphone - 如何在 iTunes Connect 中提交自动续订订阅 + 应用程序以供审核?

iphone - Xcode 4.3.2 + iOS SDK 5.1 异常断点不起作用

android - 虚拟机重载? LinearLayout背景图片

xml - 字符引用 "&#x1F"是无效的 XML 字符