Quartz2D绘图的基本使用

#pragma mark 绘制文本

- (void)drawText

{

    NSString *text = @"床上明月光,疑是地上霜.";

    UIFont *font = [UIFontsystemFontOfSize:17];

    NSDictionary *dict = @{

                           NSFontAttributeName : font,

                           NSForegroundColorAttributeName : [UIColorredColor]

                           };

    CGRect rect = [text boundingRectWithSize:CGSizeMake(20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:dict context:nil];

    //设置rect的orgin可以改变起始位置

    rect.origin = CGPointMake(100, 20);

    [text drawInRect:rect withAttributes:dict];

}

#pragma mark 画图像

- (void)drawImage

{

    UIImage *image = [UIImageimageNamed:@"头像1"];

//    //1.在指定坐标绘制

//    [image drawAtPoint:CGPointMake(80, 80)];

//    //2.在指定区域拉伸绘制

//    [image drawInRect:self.bounds];

    //3.平铺绘制

    [image drawAsPatternInRect:self.bounds];

}

#pragma mark 画圆弧

- (void)drawArc

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.添加圆弧到上下文

    CGContextAddArc(context, 160, 160, 100, -M_PI_2, 0, 1);

    //3.绘制

    CGContextDrawPath(context, kCGPathStroke);

}

#pragma mark 利用上下文画圆

- (void)drawEllipse

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.指定绘制圆形外切的矩形

    CGRect rect = CGRectMake(100, 100, 150, 150);

    //3.添加圆形到上下文路径

    CGContextAddEllipseInRect(context, rect);

    [[UIColorredColor] setStroke];

    //4.开始绘制

    CGContextDrawPath(context, kCGPathStroke);

}

#pragma mark 绘制矩形

- (void)drawRectangle

{

    //1.指定一个CGRect

    CGRect rect = CGRectMake(100, 100, 200, 200);

    [[UIColorredColor] set];

    //2.绘制实心的矩形

    UIRectFill(rect);

    //3.绘制一个空心的矩形

   CGRect rect1 = CGRectMake(150, 400, 50, 50);

    [[UIColorblueColor] set];

    UIRectFrame(rect1);

}

#pragma mark 利用上下文画一个三角形

- (void)drawTriangle

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.设置当前点

    CGContextMoveToPoint(context, 100, 100);

    //3.增加一条直线

    CGContextAddLineToPoint(context, 200, 200);

    //3.1在增加一条直线

    CGContextAddLineToPoint(context, 200, 100);

    //4.设置颜色

    [[UIColorredColor] setFill];

    //5.关闭路径

    CGContextClosePath(context);

    //6.开始绘制

    CGContextDrawPath(context, kCGPathFill);

}

#pragma mark  利用上下文绘制一条直线

- (void)drawLineWithContext

{

    //1.获取上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.设置当前点

    CGContextMoveToPoint(context, 100, 100);

    //3.增加一条直线

    CGContextAddLineToPoint(context, 200, 200);

    //4.设置颜色

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    //5.开始绘制

    CGContextDrawPath(context, kCGPathStroke);

    

}

#pragma mark 绘制一条直线

- (void)drawLine

{

    //1.获取当前图形的上下文,就是要绘制到屏幕上的

    CGContextRef context = UIGraphicsGetCurrentContext();

    //2.创建路径

    CGMutablePathRef path = CGPathCreateMutable();

        //2.1设置当前点

    CGPathMoveToPoint(path, nil, 100, 100);

        //2.2增加一条直线

    CGPathAddLineToPoint(path, nil, 200, 200);

    //3.将路径添加到上下文

    CGContextAddPath(context, path);

    //4.设置上下文

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    //5.绘制

    CGContextDrawPath(context, kCGPathStroke);

    //6.释放路径

    CGPathRelease(path);

}

转载于:https://www.cnblogs.com/yinqiang/p/3483023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值