Unicode Character Not Showing

后端 未结 4 1771
长发绾君心
长发绾君心 2020-12-17 00:59

I want to display the \"infinity\" symbol using

CGContextSelectFont(context, \"HelveticaNeue\", textSize, kCGEncodingMacRoman);    
CGContextShowTextAtPoint         


        
4条回答
  •  别那么骄傲
    2020-12-17 01:08

    It turns out that CGContextSelectFont only supports MacRoman encoding, which is basically has only a small set of characters. In order to display Unicode characters in a string, you have to use CGSetFont or the Cocoa drawing system. CGSetFont requires that you use Cocoa anyway to map characters to glyphs and then draw the glyphs using CGContextShowGlyphsAtPoint. I recommend that you look into other ways to draw these strings if you really need to display Unicode characters.

    This code basically will display the infinity symbol:

    - (void)drawRect:(CGRect)rect {
        unichar inf = 0x221E; // infinity symbol
        NSString* sName = [[NSString alloc] initWithCharacters:&inf length:1];
        UIFont* font = [UIFont fontWithName:@"Helvetica" size:32.0];
        [sName drawInRect:CGRectMake(20, 20, 40, 40)
                 withFont:font];
        [sName release];
    }
    

    Also note that on the iPhone (and on the Mac) Helvetica Neue actually does not exist... its name just maps back to standard Helvetica. See the table at http://daringfireball.net/misc/2007/07/iphone-osx-fonts for more information on available fonts on the iPhone.

提交回复
热议问题