I checked the information online. There are two methods:
Method 1: Use the Z plotting method:
-(Void) drawText :( NSString *) text x :( float) x y :( float) y {
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSelectFont (context, "Arial", 20, kCGEncodingMacRoman );
CGContextSetTextDrawingMode (context, kCGTextFill );
CGAffineTransform xform = CGAffineTransformMake (1.0, 0.0, 0.0,-1.0, 0.0, 0.0 );
CGContextSetTextMatrix (context, xform );
CGContextSetTextPosition (context, x, y + 20); // 20 is y-axis offset pixels
CGContextShowText (context, [text UTF8String], strlen ([text UTF8String]);
}
Method 2: Use the drawAtPoint method of NSString
-(Void) drawText2 :( NSString *) text x :( float) x y :( float) y {
UIFont * font = [UIFont fontWithName: @ "Arial" size: 20];
[Text drawAtPoint: CGPointMake (x, y) withFont: font];
}
Darw code in UIView class
-(Void) drawRect :( CGRect) rect {
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSetFillColorWithColor (context, [UIColor blueColor]. CGColor );
NSString * text = @ "Hello World! ";
[Self drawText: text x: 50 y: 0];
[Self drawText2: text x: 50 y: 30];
}
Both methods can be used, but the second method has lower performance than the first method.
From andy Pan's column