iOS開發UI篇—Quartz2D簡單使用(二)c#
1、畫文字spa
代碼:3d
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 YYtextview 12 13 14 - (void)drawRect:(CGRect)rect 15 { 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
效果:code
2、圖片blog
代碼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 YYimage 12 13 14 - (void)drawRect:(CGRect)rect 15 { 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
效果(拉伸圖片):string
代碼3:it
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 YYimage 12 13 14 - (void)drawRect:(CGRect)rect 15 { 16 17 // 1.加載圖片到內存中 18 UIImage *image = [UIImage imageNamed:@"me"]; 19 20 21 // 利用OC方法將圖片繪製到layer上 22 23 // 將圖片繪製到指定的位置 24 [image drawAtPoint:CGPointMake(100, 100)]; 25 }
效果(把圖片繪製到一個固定的位置):