Convert Special Characters for RTF

前端 未结 2 1174
执念已碎
执念已碎 2021-01-22 04:22

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

2条回答
  •  滥情空心
    2021-01-22 04:37

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

提交回复
热议问题