I. Draw text code: Copy code 1 // 2 // YYtextview. m 3 // 04-write text 4 // 5 // Created by Kong Zhiji on 14-6-10. 6 // Copyright (c) 2014 itcast. all rights reserved. 7 // 8 9 # import "YYtextview. h "10 11 @ implementation YYtextview12 13 14-(void) drawRect :( CGRect) rect15 {16 17 // draw the text 18 NSString * str = @ "the fan search broke up the pink Post-Russian statement offFF wafang you F reply F membership fee WFH; Fei; FN return WFH; Oh delivery; F reply; FHISFHSIFH my skin is good APIFRHi dividends AWFHIOF Weifeng I "; 19 20 // 1. obtain context 21 // CGContextRef ctx = UIGraphicsGetCurrentContext (); 22 // 2. drawing 23 // It is not recommended to use C language to draw text, because the coordinate system in quraz2d is different from the coordinate system in UIkit, And the drawn text is reversed, in addition, it is quite troublesome to draw text using the c language. 24 // CGContextSelectFont (<# CGContextRef c #>, <# const char * name #>, <# CGFloat size #>, <# CGTextEncoding textEncoding #>) 25 // CGContextShowText (ctx, <# const char * string #>,< # size_t length #>) 26 27 // draw a rectangle 28 // 1. obtain the context 29 CGContextRef ctx = UIGraphicsGetCurrentContext (); 30 // 2. plot 31 CGContextAddRect (ctx, CGRectMake (50, 50,100,100); 32 // 3. render 33 CGContextStrokePath (ctx); 34 35 36 // NSMutableDictionary * md = [NSMutableDictionary dictionary]; 37 // set the text color 38 // md [NSForegroundColorAttributeName] = [UIColor redColor]; 39 // set the text background color 40 // md [NSBackgroundColorAttributeName] = [UIColor greenColor]; 41 // set the text size 42 // md [NSFontAttributeName] = [UIFont systemFontOfSize: 20]; 43 44 // draw the text to the pointing position 45 // [str drawAtPoint: CGPointMake (10, 10) withAttributes: md]; 46 47 // draw the text within the specified range. If a row does not fit, it will automatically wrap the text. When the text exceeds the range, 48 [str drawInRect: CGRectMake (50, 50,100,100) will not be displayed) withAttributes: nil]; 49} 50 51 52 @ end 2. Image Code 1: Copy code 1 // 2 // YYimage. m 3 // 04-write text 4 // 5 // Created by Kong Zhiji on 14-6-10. 6 // Copyright (c) 2014 itcast. all rights reserved. 7 // 8 9 # import "YYimage. h "10 11 @ implementation YYimage12 13 14-(void) drawRect :( CGRect) rect15 {16 17 // 1. load the image to the memory 18 UIImage * image = [UIImage imageNamed: @ "me"]; 19 20 21 // use the drawAsPatternInRec method to draw the image to the layer, the original image is tiled by 22 [image drawAsPatternInRect: CGRectMake (0, 0,320,480)]; 23} 24 25 26 @ end copy code effect (tiled): Code 2: copy code 1 # import "YYimage. h "2 3 @ implementation YYimage 4 5 6-(void) drawRect :( CGRect) rect 7 {8 9 // 1. attach an image to memory 10 UIImage * image = [UIImage imageNamed: @ "me"]; 11 12 13 // use the OC method to draw an image to the layer 14 15 // use the drawInRect method to draw the image to the layer by stretching the original image 16 [image drawInRect: CGRectMake (0, 0,200,200)]; 17 18 // use the drawAsPatternInRec method to draw an image to the layer. The original image is tiled 19 // [image drawAsPatternInRect: CGRectMake (0, 0,320,480)]; 20} 21 22 23 @ end code 3: Copy code 1 // 2 // YYimage. m 3 // 04-write text 4 // 5 // Created by Kong Zhiji on 14-6-10. 6 // Copyright (c) 2014 itcast. all rights reserved. 7 // 8 9 # import "YYimage. h "10 11 @ implementation YYimage12 13 14-(void) drawRect :( CGRect) rect15 {16 17 // 1. loading images to memory 18 UIImage * image = [UIImage imageNamed: @ "me"]; 19 20 21 // use the OC method to draw an image to layer 22 23 // draw the image to the specified position 24 [image drawAtPoint: CGPointMake (100,100)]; 25}