Question

I'm adding chess pieces to a board and I'm using their unicode characters. However, I expect them to look like these ones from wikipedia:

wikipedia unicode chess pieces

But they are displaying like this instead:

my unicode chess pieces

The code I'm using to display these is as follows:

- (void)drawRect:(CGRect)rect
{
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle.alignment = NSTextAlignmentCenter;

    NSDictionary *attrs =
    @{ NSForegroundColorAttributeName : [UIColor blackColor],
       NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:_squareSize],
       NSParagraphStyleAttributeName:textStyle
       };

    [[self getStringForValue:_squareValue] drawInRect:rect withAttributes:attrs];

}

- (NSString*)getStringForValue:(int)value
{
    NSString* piece;

    switch (_squareValue) {
            // white pieces
        case 1:
            piece = @"\u2659";
            break;

        case 2:
            piece = @"\u2658";
            break;

        case 3:
            piece = @"\u2654";
            break;

        case 5:
            piece = @"\u2657";
            break;

        case 6:
            piece = @"\u2656";
            break;

        case 7:
            piece = @"\u2655";
            break;

            // black pieces
        case 9:
            piece = @"\u265F";
            break;

        case 10:
            piece = @"\u265E";
            break;

        case 11:
            piece = @"\u265A";
            break;

        case 13:
            piece = @"\u265D";
            break;

        case 14:
            piece = @"\u265C";
            break;

        case 15:
            piece = @"\u265B";
            break;

        default:
            piece = @"";
            break;
    }
    return piece;
}

Why do the pieces look different to wikipedia? Is it down to the font? If so, which font should I use? I've tried several fonts but each time the characters look the very same.

Était-ce utile?

La solution

The Unicode standard does not specify how a character is rendered. Judging from the Character Viewer on OS X, what you expect is "Arial Unicode MS":

enter image description here

and what you get is "Menlo Bold":

enter image description here

According to the iOS 7: Font list, apps can download "Arial Unicode MS". So you can take that font from your Mac and add it as a font resource to your app.

Autres conseils

Unicode characters only determine what the charater is supposed to represent. Fonts then implement the unicode characters. Every font will do them differently.

Try different fonts if you want, but I would recommend figuring out how to use images in your program. That is the only was to control how it looks perfectly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top