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
@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.
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;
}