转换RTF的特殊字符 [英] Convert Special Characters for RTF

查看:521
本文介绍了转换RTF的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我将特殊字符转换为可以在RTF文件中正确表示的内容吗?



我正在iPad上的字符串中存储文本并使用NSASCIIStringEncoding将其作为RTF文件输出。到现在为止还挺好。我忽略了成功,是考虑到特殊字符(例如波浪号,变音符,口音等)。对不起RoW!



最通用的RTF格式似乎需要8位文本编码与代码页转义(反斜杠后的两个十六进制数字)。我发现的唯一解决方案是转换为NSUTF8StringEncoding,然后使用stringByReplacingOccurrencesOfString,但有一个很多字符,它似乎冗长乏味,必须手动更换每一个。有没有更有效的方式逃避我? (双关语):)



感谢您的任何建议。

解决方案

检查 characterAtIndex:的值,如果它> 127,则不是ASCII,因此转义字符。



类似下面的内容

   - (NSString *)stringFormattedRTF:(NSString *)inputString 
{
NSMutableString * result = [NSMutableString string];

for(int index = 0; index< [inputString length]; index ++){
NSString * temp = [inputString substringWithRange:NSMakeRange(index,1)
unichar tempchar = [inputString characterAtIndex:index];

if(tempchar> 127){
[result appendFormat:@\\\'%02x,tempchar];
} else {
[result appendString:temp];
}
}
返回结果;
}


Can someone please assist me with converting special characters to something that can be correctly represented in an RTF file?

I am taking text stored in a string on the iPad and outputting it as an RTF file using NSASCIIStringEncoding. So far so good. What I've neglected to do successfully, is take into account special characters (e.g. tilde, umlaut, accent, etc.) . Sorry RoW!

The most universal RTF format seems to want 8-bit text encoding with code page escape (two hexadecimal digits following a backslash). So n with tilde (ñ) would be \'f1.

The only solution that occurs to me is to convert to NSUTF8StringEncoding and then use stringByReplacingOccurrencesOfString, but there are a lot characters and it seems tedious to have to replace every one of them manually. Is there a more efficient way that is escaping me? (pun intended) :)

Thanks for any suggestions.

解决方案

Check the value of characterAtIndex: if it is > 127, it is not ASCII, so escape the character.

Something like the following

- (NSString *)stringFormattedRTF:(NSString *)inputString
{
    NSMutableString *result = [NSMutableString string];

    for ( int index = 0; index < [inputString length]; index++ ) {
        NSString *temp = [inputString substringWithRange:NSMakeRange( index, 1 )];
        unichar tempchar = [inputString characterAtIndex:index];

        if ( tempchar > 127) {
            [result appendFormat:@"\\\'%02x", tempchar]; 
        } else {
            [result appendString:temp];
        }
    }
    return result;
}

这篇关于转换RTF的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆