私は次NSString
のcharactercode
ような を持っています: 0x1F514
. NSString
これを取得して別の に追加したいのですNSString
が、そのリテラル値ではなく、その背後に隠れているアイコンを使用します。この場合、ベルの絵文字です。
NSString
これを文字コードではなく絵文字に簡単に変換するにはどうすればよいですか?
私は次NSString
のcharactercode
ような を持っています: 0x1F514
. NSString
これを取得して別の に追加したいのですNSString
が、そのリテラル値ではなく、その背後に隠れているアイコンを使用します。この場合、ベルの絵文字です。
NSString
これを文字コードではなく絵文字に簡単に変換するにはどうすればよいですか?
次のようなことができます:
NSString *c = @"0x1F514";
unsigned intVal;
NSScanner *scanner = [NSScanner scannerWithString:c];
[scanner scanHexInt:&intVal];
NSString *str = nil;
if (intVal > 0xFFFF) {
unsigned remainder = intVal - 0x10000;
unsigned topTenBits = (remainder >> 10) & 0x3FF;
unsigned botTenBits = (remainder >> 0) & 0x3FF;
unichar hi = topTenBits + 0xD800;
unichar lo = botTenBits + 0xDC00;
unichar unicodeChars[2] = {hi, lo};
str = [NSString stringWithCharacters:unicodeChars length:2];
} else {
unichar lo = (unichar)(intVal & 0xFFFF);
str = [NSString stringWithCharacters:&lo length:1];
}
NSLog(@"str = %@", str);
単純に@"\u1f514"
機能しない理由は、これらの\u
値が BMP の範囲外、つまり >0xFFFF、つまり >16 ビットであってはならないからです。
したがって、私のコードはそのシナリオをチェックし、適切なサロゲート ペア マジックを実行して正しい文字列を作成します。
うまくいけば、それは実際にあなたが望むものであり、理にかなっています!
If your NSString contains this "bell" character, then it does. You just append strings the usual way, like with stringByAppendingString
.
The drawing of a bell instead of something denoting an unknown character is a completely separate issue. Your best bet is to ensure you're not using CoreText for drawing this, as it's been reported elsewhere, and I've seen it myself at work, that various non-standard characters may not work when printed that way. They do work, however, when printed with UIKit (that should be standard UI components, UIKitAdditions, and so on).
If using CoreText, you might get a bit lucky if you disable some text properties for the string with this special character, or choose appropriate font (but I won't help you here; we decided to leave the issue as Won't fix).
Having said that, the last time I was dealing with those was in pre-iOS 6 days...
Summary: your problem is not appending strings, but how you draw them.