1 #import <UIKit/UIKit.h>
2
3 @interface NJRectView : UIView
4 @end
5
6
7 #import "NJRectView.h"
8
9 @implementation NJRectView
10
11 - (void)drawRect:(CGRect)rect
12 {
13
14 // 绘制四边形
15 // 1.获取上下文
16 CGContextRef ctx = UIGraphicsGetCurrentContext();
17 // 2.绘制四边形
18 CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));
19
20 // 如果要设置绘图的状态必须在渲染之前
21 // CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
22 // 绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态
23 // CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
24
25 // 调用OC的方法设置绘图的颜色
26 // [[UIColor purpleColor] setFill];
27 // [[UIColor blueColor] setStroke];
28 // 调用OC的方法设置绘图颜色(同时设置了实心和空心)
29 // [[UIColor greenColor] set];
30 [[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];
31
32 // 3.渲染图形到layer上
33 CGContextStrokePath(ctx);
34 // CGContextFillPath(ctx);
35
36 }
![]()