3

I have an NSString with a charactercode like this: 0x1F514. I want to take this NSString and add it to another NSString, but not with the literal value of it, but the icon hidden behind it. In this case an emoticon of a bell.

How can I easily convert this NSString to show the emoticon instead of the character code?

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
  • Is this emoticon an encoded image? – pro_metedor Oct 18 '12 at 20:22
  • I assume he means he code point, 0x1f514, which is this: http://www.fileformat.info/info/unicode/char/1f514/index.htm . Although, that's not an "emoticon". – mattjgalloway Oct 18 '12 at 20:27
  • I am using this code to show an emoticon from the iPhone-emoji-keyboard. Not sure if I am using the correct terminology :/ – Simen Øian Gjermundsen Oct 18 '12 at 20:31
  • 0x1F514 is a "bell" yes, from the commonly known "Emoji" range. I wrote an answer but it was completely wrong. I'll fix and update! – mattjgalloway Oct 18 '12 at 20:34
  • So you have an string like "" (if you've got the appropriate font the there will be [a bell in those quotes](https://dl.dropbox.com/u/7511537/Post%20Content/Bell%20character.png)) and you want to add it to another string... you mean like concatenate the strings? "A" + "B" = "AB"? What do you mean 'the icon hidden behind it?' Where are you trying to display the character and what are you doing that's going wrong? – bames53 Oct 19 '12 at 01:52
  • I am sorry if I was a bit unclear. I start off with a string. An NSString, containing these characters (inside the "s) : "\U0001F449" or "\u1F449". I want to take that string and print it in a UITextView. I want the emoji representation to show up in the textview. – Simen Øian Gjermundsen Oct 19 '12 at 07:21
  • If you have a string literal @"\U0001F449" (not "\u1F449") then the string contains the character you want; no special handling needed. Just insert the string into the TextView normally. – bames53 Oct 23 '12 at 18:52

2 Answers2

2

Something like this would do:

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

The reason simply @"\u1f514" doesn't work is because those \u values cannot be outside the BMP, i.e. >0xFFFF, i.e. >16-bit.

So, what my code does is check for that scenario and does the relevant surrogate pair magic to make the right string.

Hopefully that is actually what you want and makes sense!

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
1

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.

dda
  • 6,030
  • 2
  • 25
  • 34
Piotr Kalinowski
  • 2,272
  • 1
  • 14
  • 15
  • 1
    Ok. I am trying to draw this text by setting the .text-property of a UITextView. Does this make any difference? – Simen Øian Gjermundsen Oct 18 '12 at 20:44
  • We are not using standard iOS components, so I never had to learn how exactly they work ;) Emoji is the key word though. See comments to this question for example: http://stackoverflow.com/questions/9940278/why-cant-emoji-display-correctly-in-uitextfield This looks like exactly your problem with solution presented. – Piotr Kalinowski Oct 18 '12 at 20:49
  • 2
    Isn't the question that the string is actually "0x1F514", not actually the "bell" character? That's what I understood anyway. – mattjgalloway Oct 18 '12 at 21:08
  • That would be rather unusual interpretation of character code. I demand code. – Piotr Kalinowski Oct 19 '12 at 07:50
  • 1
    It's not an unusual interpretation of character code, I'd say. 0x1F514 is a unicode code point. – mattjgalloway Oct 19 '12 at 20:17
  • Yes, but when I say the string contains character code, I mean the string contains character with given character code, and not hexadecimal string that should still be converted to actual character. – Piotr Kalinowski Oct 20 '12 at 00:12