一 概述
二 绘制文字
2.1 代码文件
- (void)drawRect:(CGRect)rect {
// Drawing code
NSString *text=@"hello world";
CGRect textFrame=CGRectMake(0, 0, 250, 250);
NSDictionary *dict=@{
NSFontAttributeName:[UIFont systemFontOfSize:40],
NSForegroundColorAttributeName:[UIColor redColor],
NSStrokeWidthAttributeName:@10
};
//UIRectFill(textFrame);
//[text drawInRect:textFrame withAttributes:dict];
[text drawAtPoint:CGPointZero withAttributes:dict];
}
2.2 代码说明
- UIRectFill:绘制矩形区域
text drawInRect
:绘制文字时,会自动换行text drawAtPoint
:绘制文字时,不会自动换行
2.3 效果图
三 绘制图像
3.1 一般绘制
代码
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *image=[UIImage imageNamed:@"dog"];
[image drawAtPoint:CGPointZero];
//[image drawInRect:CGRectMake(0, 0, 100, 100)];
}
效果图
3.2 平铺和裁剪
代码
- (void)drawRect:(CGRect)rect {
// Drawing code
UIRectClip(CGRectMake(0, 0, 100, 100)); //裁剪大小
UIImage *pImage=[UIImage imageNamed:@"dog"];
[pImage drawAsPatternInRect:rect];
}
效果图