drawRect中一些方法的调用

常会遇到一种情况,在drawRect中用上下文绘制了一个图形,当运行时发现除了自己的图形外,其他的背景都是黑的。这是因为没有给其他的区域设置颜色

设置方法:

-(void)drawRect:(CGRect)rect{
    //为rect设置整体背景颜色
    [[UIColor greenColor] setFill];
    UIRectFill(rect);

}

 

上下文只有在drawRect中获得才有作用,其他方法中获取值为空

drawRect中绘图,可以使用上下文,也可以使用CAShapeLayer进行绘制,layer绘制的话,有一个好处就是layer有个

zPosition属性(值越大显示越靠上),可以动态的设置 layer的层级,而上下文绘制是按先绘制的在下,层级不好控制

两种方式划线

layer:

 CAShapeLayer *lineLayer = [CAShapeLayer layer];
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    //线
    [linePath moveToPoint:startPoint];
    [linePath addLineToPoint:endPoint];
    
    //将绘制路径赋值给layer
    lineLayer.path = linePath.CGPath;
    //设置线的颜色    
    lineLayer.strokeColor = [UIColor redColor].CGColor;
    //设置线头部类型
    lineLayer.lineCap       = kCALineCapSquare;
    //设置线宽
    lineLayer.lineWidth = 10;
    //设置线的层级
    lineLayer.zPosition = 251;
    
    //添加到layer上
    [self.layer addSublayer:lineLayer];

上下文:

//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//上下文移动到画布点sPoint
CGContextMoveToPoint(context, sPoint.x, sPoint.y);
//给上下文添加线到点ePoint
CGContextAddLineToPoint(context, ePoint.x, ePoint.y);

//设置颜色        
CIColor * bclor = [[CIColor alloc] initWithColor:edg.beginColor];
CGContextSetStrokeColor(context, bclor.components);
//也可以
[[UIColor blueColor] setStroke];
//
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0, 1);

//设置线宽
CGContextSetLineWidth(context, edg.width);
//设置绘图方式并绘图stroke描边 fill填充      
CGContextDrawPath(context, kCGPathFillStroke);

其他的图形绘制类似,写文字功能

layer:

CATextLayer * textLayer = [CATextLayer layer];
        
//解决文字不清晰问题        
textLayer. wrapped = YES ; 
//设置字体大小        
textLayer.fontSize = 22;
//设置显示文字        
textLayer.string = name;
//设置文字颜色        
textLayer.foregroundColor = roadColor.CGColor;
//设置背景颜色
textLayer.backgroundColor = [UIColor lightGrayColor].CGColor;
//设置layer的frame
textLayer.frame = frame;

//添加到layer上
[self.layer addSublayer:textLayer]

上下文:

//获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();

//设置字号
float fontSize =14.0f;

/*这种方式写出来的文字是反着的,需要对其进行旋转*/
CGContextSetTextMatrix(content, CGAffineTransformMakeScale(1.0, -1.0));
//设置字体颜色
[[UIColor redColor] set];

//字体和字号赋值
CGContextSelectFont(ctx, "Helvetica", fontSize, kCGEncodingMacRoman);

//设置填充模式
CGContextSetTextDrawingMode(ctx, kCGTextFill);

//写文字方法
CGContextShowTextAtPoint(ctx, p4.x, p4.y, [strAngle UTF8String], [strAngle length]);

绘制椭圆

    //获取上下文
    CGContextRef con=UIGraphicsGetCurrentContext();
    //在规定的矩形区内绘制内切椭圆
    CGContextAddEllipseInRect(con, CGRectMake(0, 0, 200, 80));
    //设置填充颜色
    CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
    //绘制
    CGContextFillPath(con);

 

转载于:https://www.cnblogs.com/absty-guo/p/5869338.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值