(IPhone/iPad Development) Draw text on UIView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.