I’m working on a Text Addition feature for my App. This text is going to be added with CGContext and Quartz 2D. The variable text is pulled from a UITextField’s text property.
Here is the code I have so far:
CGRect textBox = CGRectMake(0, 0, 300, 200);
UIView *textView = [[UIView alloc] initWithFrame:textBox];
const char *textChar = [text UTF8String];
NSInteger xLocation = (contentOffset.x + 100);
CGContextSelectFont (UIGraphicsGetCurrentContext(), "Helvetica", 14.0f, kCGEncodingMacRoman);
CGContextSetShouldSmoothFonts(UIGraphicsGetCurrentContext(), YES);
CGContextSetCharacterSpacing (UIGraphicsGetCurrentContext(), 10);
CGContextSetTextDrawingMode (UIGraphicsGetCurrentContext(), kCGTextFill);
CGContextSetRGBFillColor (UIGraphicsGetCurrentContext(), 255, 255, 255, 1.0);
CGContextShowTextAtPoint (UIGraphicsGetCurrentContext(), xLocation, 110, textChar, 9);
[drawGallery addSubview:textView];
I have a few problems with this:
-
The text is rendering flipped – (this is easily rectified with a transform, but the tutorial I followed didn’t have this issue so I’m confused a little LINK).
-
The text is rendering with strange characters – I think this is an encoding issue, but how do i adjust this?
-
The text doesn’t get added to the view at the
addSubviewline, it only gets added when the CGContext is updated, i.e. I have a draw lines function too and if I draw on the view then the text magically pops up. -
The text is horribly jagged and badly rendered, is there something I can do for this? I’ve added a
CGContextSetShouldSmoothFontsbut it doesn’t seem to be doing anything.
See below for an example:

Thanks for any help you can give.
For the text flipping, a solution to someone with the same problem was posted text inverted problem solution [Stackoverflow] – use:
For the encoding problem, you are getting the text cString with
[text UTF8String](UTF8 characters are in general multibyte) butCGContextSelectFont()is specifying MacRoman (a single byte format).Julian