将特殊字符转换为 RTF

发布于 2024-09-19 17:45:46 字数 394 浏览 2 评论 0原文

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

我正在 iPad 上获取存储在字符串中的文本,并使用 NSASCIIStringEncoding 将其输出为 RTF 文件。到目前为止,一切都很好。我忽略的成功做法是考虑特殊字符(例如波形符、元音变音、重音等)。对不起罗!

最通用的 RTF 格式似乎需要带有代码页转义的 8 位文本编码(反斜杠后面的两个十六进制数字)。因此带有波形符 (ñ) 的 n 将为 \'f1。

我想到的唯一解决方案是转换为 NSUTF8StringEncoding,然后使用 stringByReplacingOccurrencesOfString,但是字符很多,必须手动替换其中的每一个似乎很乏味。有没有更有效的方法让我逃避? (双关语):)

感谢您的任何建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2024-09-26 17:45:46

@falconcreek 的回答节省了我大量的时间来编写代码来应对更广泛的情况,包括中文字符(按照 DenVog 的要求)。特别是,检查“\”、“{”和“}”非常重要,因为 RTF 格式使用这些字符。 (参见如何将unicode字符串输出为RTF(使用C#)<例如 /a>。)NSString 上的以下类别处理如下字符串:

快\慢的{棕色}狐狸懒洋洋地“咕噜咕噜”地喝着他的π拿铁,而王菲则在后台演奏。

@interface NSString (TR)    
- (NSString *)stringFormattedRTF;
@end

@implementation NSString (TR)

#define backslash 0x5C
#define openCurlyBrace 0x7B
#define closeCurlyBrace 0x7D

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

    for (int index = 0; index < [self length]; index++)
    {
        unichar unicodeCharacter = [self characterAtIndex: index];

        if (unicodeCharacter == backslash || unicodeCharacter == openCurlyBrace || unicodeCharacter == closeCurlyBrace)
        {
            [result appendFormat: @"\\%c", unicodeCharacter];

        }
        else if (unicodeCharacter > 127)
        {
            [result appendFormat:@"\\uc0\\u%u ", unicodeCharacter];
        }
        else
        {
            [result appendFormat:@"%c", unicodeCharacter];
        }
    }
    return result;
}

旁注: Microsoft 提供 1.9.1 RTF 规范,如果你想输出 RTF,这真的很有帮助。维基百科称(截至 2012 年 5 月)这是最新版本。 Google 倾向于采用更旧的 RTF 规范。

@falconcreek's answer saved me lots of time writing code to coping with a wider range of cases, including, say, Chinese characters (as requested by DenVog). In particular, it's important to check for: "\", "{" and "}" as these are used by the RTF format. (See How to output unicode string to RTF (using C#), for example.) The following category on NSString copes with a string such as:

The quick \ slow {brown} fox “slurped” lazily on his π-latté, while Faye Wong (王菲) played in the background.

@interface NSString (TR)    
- (NSString *)stringFormattedRTF;
@end

@implementation NSString (TR)

#define backslash 0x5C
#define openCurlyBrace 0x7B
#define closeCurlyBrace 0x7D

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

    for (int index = 0; index < [self length]; index++)
    {
        unichar unicodeCharacter = [self characterAtIndex: index];

        if (unicodeCharacter == backslash || unicodeCharacter == openCurlyBrace || unicodeCharacter == closeCurlyBrace)
        {
            [result appendFormat: @"\\%c", unicodeCharacter];

        }
        else if (unicodeCharacter > 127)
        {
            [result appendFormat:@"\\uc0\\u%u ", unicodeCharacter];
        }
        else
        {
            [result appendFormat:@"%c", unicodeCharacter];
        }
    }
    return result;
}

Side note: Microsoft provide 1.9.1 RTF spec, which is really helpful if you want to output RTF. Wikipedia says (as of May 2012) this the most recent version. Google tends to kick up a much older RTF specs.

幽梦紫曦~ 2024-09-26 17:45:46

检查 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];
        }
    }
    return result;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文