demo:[https://github.com/asd521411/iOS-CGContextRefDemo.git]
//
// CustomView.m
// CGContextRefDemo
//
// Created by hongbaodai on 2018/12/10.
// Copyright © 2018年 caomaoxiaozi. All rights reserved.
//
#import “CustomView.h”
#define PI 3.14159265358979323846
@implementation CustomView
-
(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
} -
(void)drawRect:(CGRect)rect {
[self drawCharacter];
} -
(void)drawCharacter {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);//设置颜色
//CGFontRef fontRef = UIGraphi
//CGContextSetFont(context, <#CGFontRef _Nullable font#>)
UIFont font = [UIFont systemFontOfSize:14];
//[@“1” drawInRect:CGRectMake(10, 40, 100, 40) withFont:font];//废弃
[@“画图1” drawInRect:CGRectMake(10, 40, 100, 40) withAttributes:@{NSAttachmentAttributeName:font,NSForegroundColorAttributeName:[UIColor cyanColor]}];
//画图
//画圆
CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);//设置填充的画笔颜色
//CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y为圆点坐标,radius半径,s