CGContextRef 使用小记

1. 用CGContextRef 画文字

在 UIView的 - (void)drawRect:(CGRect)rect {} 方法中进行

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(context, YES); // 抗锯齿
    CGContextSetLineWidth(context, 1.0f); //设置线宽

    /*写文字*/
        CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色
        UIFont  *font = [UIFont boldSystemFontOfSize:15.0];//设置文本字体大小
        NSDictionary *attribute = @{NSFontAttributeName:wordFont,NSForegroundColorAttributeName: fontColor};
        [@"文字" drawInRect:CGRectMake(10, 20, 80, 20) withAttributes:attribute];
2. 画圆

    /** 画圆 */
    //void 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半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。

CGContextSetRGBStrokeColor(context,1,0,0,1.0);//画笔线的颜色 不设置的话是黑色

猜你喜欢

转载自www.cnblogs.com/liuw-flexi/p/9029168.html
今日推荐