0

I'm trying to display the "treble clef" in a UILabel, but it doesn't work. However, NSLog is able to display it.

NSString *strM = @"\U0001d11e";
NSLog(@"treble clef works: %@",strM); //works
NSLog(@"works: "); //works

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
[self.view addSubview:lbl];
lbl.text = strM; //shows "empty" or (box around a question mark)

What am I missing to display that Unicode character in a UILabel?

jdl
  • 6,151
  • 19
  • 83
  • 132

2 Answers2

1

Since Xcode happens to use a font which has a glyph for the treble clef character, it shows up in the NSLog message.

However, the font that UILabel is using doesn't happen to contain a glyph for that particular Unicode character. That's why you're seeing a "box around a question mark."

If you try a more common Unicode character, you'd see there is no problem with the code itself.

One option is to switch to a different font which supports that particular character.

If that isn't possible, you could try to use an NSAttributedString with an inline image of the treble clef.

Community
  • 1
  • 1
0
  • Had to look into the Fonts that support Treble Clef as PetehChristian(gave him a point) said. http://www.fileformat.info/info/unicode/char/1d11e/fontsupport.htm Liked Symbola: http://users.teilar.gr/~g1951d/Symbola.zip
  • Unzipped the file and dragged it to Xcode: Supporting Files. Bundled it into the target.
  • Make sure in the BuildPhases:Copy Bundle Resources.
  • Had to also put in the Info.plist: add(Fonts Provided by application). And add the file: Symbola.ttf.
  • Then verified the font was in the system:

    NSArray *fontFamilies = [UIFont familyNames];
    for (int i = 0; i < [fontFamilies count]; i++)
    {
        NSString *fontFamily = [fontFamilies objectAtIndex:i];
        NSArray *fontNames = [UIFont fontNamesForFamilyName:[fontFamilies objectAtIndex:i]];
        NSLog (@"%@: %@", fontFamily, fontNames);
    }
    
  • subclassed UIView:(this is best for me anyway)

    [(NSString*) @"\U0001d11e" drawInRect:CGRectMake(50+10, 50-15, 80, 20) withFont:[UIFont boldSystemFontOfSize:100]];

  • To put out the unicode to label:(still issues here)

    [lbl setFont:[UIFont fontWithName:@"Symbola.ttf" size: 40]];
    lbl.text = @"\U0001D11E";
    
jdl
  • 6,151
  • 19
  • 83
  • 132