I am writing a simple practice. However, my text is flip upside 开发者_StackOverflow社区down when I trying to use CGContext to put some string on the UIView, I am wondering why and how to change it into correct format.
Here is my code in the drawRect
char *string = "TEST";
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSelectFont (context,"Helvetica-Bold",12, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, 5, 5, string, strlen(string));
CGContextClosePath(context);
Thanx for your help.
CoreGraphics uses cartesian coordinates, so you need to translate your context before doing any drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// transforming context
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// your drawing code
Quartz2D has an inverted y axis - handy eh? If you are inside a drawRect method, you can use the following to flip the text over.
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
Another way is;
transform = CGAffineTransformMake(1.0,0.0,0.0,-1.0,0.0,0.0);
CGContextSetTextMatrix(context, transform);
Or on one line;
CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));
Use the UIKit additions in NSString:
NSString *string = @"TEST";
[string drawAtPoint:CGPointMake(5, 5)
withFont:[UIFont boldSystemFontOfSize:12]];
精彩评论