views:

2332

answers:

3

I want to display the "infinity" symbol using

CGContextSelectFont(context, "HelveticaNeue", textSize, kCGEncodingMacRoman);    
CGContextShowTextAtPoint(context, myCenter.x, myCenter.y + textHeight, [sName     cStringUsingEncoding:NSMacOSRomanStringEncoding], [sName length]);

It is displayed as a square box, or a circle. I have found out this symbol is in decimal 176 and 221E in Hexadecimal format. I am using Helvetica as my font, and have tried others with no luck. Is this a problem with the encoding I am using?

+3  A: 

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.

Jason Coco
This is how I initialize sName: NSString *sName = [NSString stringWithUTF8String:"\xE2\x88\x9E"];And have change what you suggest here, and I dont get the square or blank, I get a strange character, but it is not the infinity symbol. What else am I missing?
Carlos Hernandez
That's really strange... I get the proper character here. What symbol are you getting?
Jason Coco
It works now. Thanks. Performance wise, which is better?
Carlos Hernandez
They've done a lot to make the AppKit and UIKit add-ons to NSString for drawing perform better... but using the CGContext functions will usually out-perform the UIKit functions. That said, my advice is to do it the easiest way and then tune it if you actually have performance issues.
Jason Coco
+2  A: 

It's worth noting that you can still use the CGContext functions alongside -[NSString drawWithRect:] and its cousins. For example, the following code draws a string (which can include any characters, including the infinity symbol which started this thread) with 50%-opaque black:

CGContextSaveGState(context);
CGContextSetRGBFillColor(context, 0, 0, 0, 0.5);
[self.text drawInRect:targetRect withFont:self.font];
CGContextRestoreGState(context);
A: 

You want to avoid drawInRect:withFont: (in MonoTouch: DrawString (string, RectangelF, UIFont font) as this API up until iPhoneOS 4.0 does not render strings that might contain certain unicode characters (like dingbats) and will silently cut rendering at that point.

Instead you must use drawInRect:withFont:lineBreakMode:alignment: (in MonoTouch this is the DrawString (string msg, RectangleF rect, UIFont font, UILineBreakMode breakMode, UITextAlignment align) method) as this renders properly.

In addition, not all fonts on the iPhone is a complete set of fonts, you can use the following reference:

http://daringfireball.net/misc/2007/07/iphone-osx-fonts

For a list of fonts that have the complete set of characters.

miguel.de.icaza