//绘制多图形(继承UIView)
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor redColor];
[color set];
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 5.0;
//创建多边形
[path moveToPoint:CGPointMake(100, 0)];
//画线
[path addLineToPoint:CGPointMake(200, 40)];
[path addLineToPoint:CGPointMake(160, 140)];
[path addLineToPoint:CGPointMake(40, 140)];
[path addLineToPoint:CGPointMake(0, 40)];
//封口
[path closePath];
//渲染
[path fill];
}
//绘制圆角矩形
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor redColor];
[color set];
//圆角矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 50, 80, 80) cornerRadius:10];
path.lineWidth = 5.0;
//渲染
[path fill];
//练习:用该方法 创建一个 空心的 圆角的矩形。
//圆角矩形
UIBezierPath *path1 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 180, 180, 80) cornerRadius:10];
path1.lineWidth = 5.0;
//渲染
[path1 stroke];
}
//IOS6下绘制文字
- (void)drawRect:(CGRect)rect
{
//1.绘制简单的字符串
NSString *string1 = @"Hello World!!";
//drawPoint: withFont的方法进行字符串的绘制
[string1 drawAtPoint:CGPointMake(20, 64) withFont:[UIFont systemFontOfSize:30]];
//2.在制定的矩形框中绘制字符串
NSString *string2 = @"这是一个Loooooooooooooooooooooooooooooong字符串";
//drawInRect: withFont
[string2 drawInRect:CGRectMake(20, 100, 200, 300) withFont:[UIFont systemFontOfSize:28]];
//3.当你不知道字符串的长度 很长很长很长,根据字符的高度来计算它所占的矩形的高。
NSString *string3 = @"今天是好日子,心想的事儿都能成!,想成什么事呢?iOS8.1 昨天晚上被中国的盘古团队在全球首先越狱成功。这标志着中国移动互联网安全行业 已经进入到世界领先水平。";
UIFont *font = [UIFont systemFontOfSize:20];
//根据字体的大小算高度
CGSize size = [string3 sizeWithFont:font constrainedToSize:CGSizeMake(280, 640)];
//根据字符串的大小 开始绘制字符串
[string3 drawInRect:CGRectMake(20, 380, size.width, size.height) withFont:font];
//根据字符串的长度 来动态的计算索要绘制出所有内容所需要的size;向下兼容(4s 4)
}
//IOS7下绘制文字
- (void)drawRect:(CGRect)rect
{
//1.简单的字符串
NSString *string1 = @"今天又有雾霾了,买了个口罩6元,今天中午吃什么,天冷楼下西部马华吃牛肉面,尼玛,太坑了 5元 给一片肉!";
//iOS7中 废弃了iOS6中的方法
[string1 drawInRect:CGRectMake(20, 32, 280, 300) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28],NSForegroundColorAttributeName:[UIColor redColor]}];
//2.自定适应高度
NSString *weibo =@"热烈庆祝APEC会议在北京召开,人家都放假,我们不放!呵呵S。这都不是事,是事事苹果公司规定2015-2开始凡是不适配64位处理器的应用程序全部下架!,缓冲时间4个月。";
//创建一个结构体
CGRect frame = [weibo boundingRectWithSize:CGSizeMake(300, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blueColor]} context:nil];
//没有指定坐标
frame.origin = CGPointMake(20, 300);
//开始绘制
[weibo drawInRect:frame withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blueColor]}];
}
//剪裁图片
- (void)drawRect:(CGRect)rect
{
//创建图片的对象
UIImage *image = [UIImage imageNamed:@"5.jpg"];
//创建一个贝赛尔路径
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 50, 180, 180)];
//剪切图片---->按钮画的圆进行裁剪
[path addClip];
//绘制图片
[image drawInRect:CGRectMake(50, 150, 160, 160)];
}
//加载图形(继承UIViewController)
- (void)viewDidLoad
{
[super viewDidLoad];
TRMyView *myView = [[TRMyView alloc] initWithFrame:self.view.frame];
[self.view addSubview:myView];
}
//加载到主窗口(AppDelegate)
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[TRRootViewController alloc] init];