一、畫文字 代碼: 複製代碼 1 // 2 // YYtextview.m 3 // 04-寫文字 4 // 5 // Created by 孔醫己 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 // 畫文字18 NSString *str = @"的額搜風搜分手了粉色發俄雙方說法offFF瓦房你F回複F入會費WFH;飛;FN返回WFH;哦發貨;F回複;FHISFHSIFH我皮膚好APIFRHi分紅AWFHIOF威鋒網i";19 20 // 1.擷取上下文21 // CGContextRef ctx = UIGraphicsGetCurrentContext();22 // 2.繪圖23 // 不推薦使用C語言的方法繪製文字, 因為quraz2d中的座標系和UIkit中的座標系不一致, 繪製出來的文字是顛倒的, 而且通過C語言的方法繪製文字相當麻煩24 // CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)25 // CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)26 27 // 繪製矩形28 // 1.擷取上下文29 CGContextRef ctx = UIGraphicsGetCurrentContext();30 // 2.繪圖31 CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));32 // 3.渲染33 CGContextStrokePath(ctx);34 35 36 // NSMutableDictionary *md = [NSMutableDictionary dictionary];37 // // 設定文字顏色38 // md[NSForegroundColorAttributeName] =[UIColor redColor];39 // // 設定文字背景顏色40 // md[NSBackgroundColorAttributeName] = [UIColor greenColor];41 // // 設定文字大小42 // md[NSFontAttributeName] = [UIFont systemFontOfSize:20];43 44 // 將文字繪製到指點的位置45 // [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];46 47 // 將文字繪製到指定的範圍內, 如果一行裝不下會自動換行, 當文字超出範圍後就不顯示48 [str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];49 }50 51 52 @end 二、圖片 代碼1: 複製代碼 1 // 2 // YYimage.m 3 // 04-寫文字 4 // 5 // Created by 孔醫己 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.載入圖片到記憶體中18 UIImage *image = [UIImage imageNamed:@"me"];19 20 21 // 利用drawAsPatternInRec方法繪製圖片到layer, 是通過平鋪原有圖片22 [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];23 }24 25 26 @end複製代碼效果(平鋪): 代碼2: 複製代碼 1 #import "YYimage.h" 2 3 @implementation YYimage 4 5 6 - (void)drawRect:(CGRect)rect 7 { 8 9 // 1.載入圖片到記憶體中10 UIImage *image = [UIImage imageNamed:@"me"];11 12 13 // 利用OC方法將圖片繪製到layer上14 15 // 利用drawInRect方法繪製圖片到layer, 是通過展開原有圖片16 [image drawInRect:CGRectMake(0, 0, 200, 200)];17 18 // 利用drawAsPatternInRec方法繪製圖片到layer, 是通過平鋪原有圖片19 // [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];20 }21 22 23 @end 代碼3: 複製代碼 1 // 2 // YYimage.m 3 // 04-寫文字 4 // 5 // Created by 孔醫己 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.載入圖片到記憶體中18 UIImage *image = [UIImage imageNamed:@"me"];19 20 21 // 利用OC方法將圖片繪製到layer上22 23 // 將圖片繪製到指定的位置24 [image drawAtPoint:CGPointMake(100, 100)];25 }