iOS開發UI篇—Quartz2D簡單使用(二)


iOS開發UI篇—Quartz2D簡單使用(二)

一、畫文字

代碼:

 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

效果:

二、圖片

代碼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

效果(拉伸圖片):

代碼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 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     }

效果(把圖片繪制到一個固定的位置):


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM