I have a string in my cocoa GUI that needs to have special formatting (fonts, colors, etc.). Naturally, I’m using an attributed string. For convenience, I Init the string as an RTF:
NSString *inputString = @"This string has special characters";
NSString *rtfString = [NSString stringWithFormat:@"{@"***LENGTHY RTF FORMATTING STRING *** %@", inputString];
NSAttributedString *testString = [[NSAttributedString alloc] initWithRTF:[rtfString dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:nil];
The problem is, the “inputString” might have special characters, which are not displayed properly due to the UTF8Encoding. They’re replaced with other symbols.
é is left as Å©.
So, right now I’m doing this:
NSData* intermediateDataString=[inputString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
inputString = [[[NSString alloc] initWithData:intermediateDataString encoding:NSUTF8StringEncoding] autorelease];
This does not display the unexpected characters, but it does remove all accents and leaves in their stead the unaccented letter – é is left as e.
This is an improvement since everything can be read, but it is far from ideal.
Thoughts?
I would do something like this. First, create a dummy attributed string:
and obtain the attributes:
Now I will use this attribute to create another attributed string:
Another approach is to use HTML instead of RTF; then you can include non-ascii characters as unicode in it.