How to show or convert (non-standard) \U0099 for Trademark "TM" Character?

I've ran into some issue displaying the trademark "TM" character on my UILabel.

The "TM" character having problem showing up is \U0099 instead of the usual \U2122
I dig a little deeper and find out the "TM" character \U0099 belongs to a very few Chinese fonts.

So I'm guessing iOS doesn't have the font to show it in labels or does not recognize it at all.

I've tried to scan my data for "\U0099" and stringreplace it to \U2122, but seems like NSString functions will escape unicode characters automatically so this "TM" character won't even be there.

Has anyone encountered this issue before or can give me suggestions as to how to deal with this \U0099 character?

Thanks in advance


Solution 1:

It is unclear to me how you've obtained your NSString or what you have actually tried to solve your problem. So this suggestion might be completely unsuitable, but let's see if it helps...

U+0099 is an unassigned Unicode control character, it is not a TM symbol. It is fairly hard to get this character into an NSString as Clang at least objects if you place the escape into a literal, and Cocoa fails to translate a sequence of bytes in UTF-8 into an NSString if it contains it. This problem might be what is behind your comment that you could not string replace it.

However starting with UTF-16, I did manage to create a string with U+0099 in it:

unichar b[] = { 0x61, 0x62, 0x63, 0x99, 0x64, 0x65, 0x66 };
NSString *s = [[NSString alloc] initWithBytes:b length:14 encoding:NSUTF16LittleEndianStringEncoding];

That is the string "abc\U0099def" (calling characterAtIndex:3 will show you this).

Using the same approach an NSString with just U+0099 in it can be generated:

unichar notTMChar = 0x99;
NSString *notTMStr = [[NSString alloc] initWithBytes:&notTMChar length:2 encoding:NSUTF16LittleEndianStringEncoding];

and that can be used in a string replace call:

NSString *t = [s stringByReplacingOccurrencesOfString:notTMStr withString:@"™"];

giving t the value "abc™def" as required.

Warning: We are dealing with an unassigned Unicode control character here. Clang/Cocoa rejected it in UTF-8, it is probably unintentional that it accepted it in UTF-16. Using C library functions to do this is probably more reliable. Xcode 5.1.1 with Clang 5.1 was used for the tests.

HTH