1. 程式人生 > >iOS開發UI篇—Quartz2D簡單使用(二)

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

title kit size reat img ref com 繪圖 設置

一、畫文字

代碼:

技術分享圖片

//
// YYtextview.m
// 04-寫文字
//
// Created by 孔醫己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYtextview.h"

@implementation YYtextview


- (void)drawRect:(CGRect)rect
{

// 畫文字
NSString *str = @"的額搜風搜分手了粉色發俄雙方說法offFF瓦房你F回復F入會費WFH;飛;FN返回WFH;哦發貨;F回復;FHISFHSIFH我皮膚好APIFRHi分紅AWFHIOF威鋒網i";

// 1.獲取上下文
// CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.繪圖
// 不推薦使用C語言的方法繪制文字, 因為quraz2d中的坐標系和UIkit中的坐標系不一致, 繪制出來的文字是顛倒的, 而且通過C語言的方法繪制文字相當麻煩
// CGContextSelectFont(<#CGContextRef c#>, <#const char *name#>, <#CGFloat size#>, <#CGTextEncoding textEncoding#>)
// CGContextShowText(ctx, <#const char *string#>, <#size_t length#>)

// 繪制矩形
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.繪圖
CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
// 3.渲染
CGContextStrokePath(ctx);


// NSMutableDictionary *md = [NSMutableDictionary dictionary];
// // 設置文字顏色
// md[NSForegroundColorAttributeName] =[UIColor redColor];
// // 設置文字背景顏色
// md[NSBackgroundColorAttributeName] = [UIColor greenColor];
// // 設置文字大小
// md[NSFontAttributeName] = [UIFont systemFontOfSize:20];

// 將文字繪制到指點的位置
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];

// 將文字繪制到指定的範圍內, 如果一行裝不下會自動換行, 當文字超出範圍後就不顯示
[str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];
}


@end

技術分享圖片

效果:

技術分享圖片

二、圖片

代碼1:

技術分享圖片

//
// YYimage.m
// 04-寫文字
//
// Created by 孔醫己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{

// 1.加載圖片到內存中
UIImage *image = [UIImage imageNamed:@"me"];


// 利用drawAsPatternInRec方法繪制圖片到layer, 是通過平鋪原有圖片
[image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}


@end

技術分享圖片

效果(平鋪):

技術分享圖片

代碼2:

技術分享圖片

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{

// 1.加載圖片到內存中
UIImage *image = [UIImage imageNamed:@"me"];


// 利用OC方法將圖片繪制到layer上

// 利用drawInRect方法繪制圖片到layer, 是通過拉伸原有圖片
[image drawInRect:CGRectMake(0, 0, 200, 200)];

// 利用drawAsPatternInRec方法繪制圖片到layer, 是通過平鋪原有圖片
// [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];
}


@end

技術分享圖片

效果(拉伸圖片):

技術分享圖片

代碼3:

技術分享圖片

//
// YYimage.m
// 04-寫文字
//
// Created by 孔醫己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{

// 1.加載圖片到內存中
UIImage *image = [UIImage imageNamed:@"me"];


// 利用OC方法將圖片繪制到layer上

// 將圖片繪制到指定的位置
[image drawAtPoint:CGPointMake(100, 100)];
}

技術分享圖片

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

技術分享圖片

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