1、畫文字c#
代碼:spa
//
// YYtextview.m
// 04-寫文字
//
// Created by 孔醫己 on 14-6-10.
// Copyright (c) 2014年 itcast. All rights reserved.
//code
#import "YYtextview.h"blog
@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];
}內存
@endstring
效果:it
2、圖片io
代碼1:table
//
// 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)];
}
效果(把圖片繪製到一個固定的位置):