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

來源:互聯網
上載者:User

一、畫文字 代碼: 複製代碼 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     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.