chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. iOS
  3. 正文

swift画线

2016年 9月 11日 80点热度 0人点赞 0条评论
    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineCap(context,CGLineCap.Round)//
        CGContextSetLineWidth(context, 2)// 线宽
        CGContextSetAllowsAntialiasing(context, true)// 锯齿
        CGContextSetRGBStrokeColor(context, 231/255, 231/255, 231/255, 1)//颜色
        CGContextBeginPath(context)
        CGContextMoveToPoint(context, 0, 0)// 起点坐标
        CGContextAddLineToPoint(context, self.frame.width, 0)// 终点坐标
        CGContextStrokePath(context)
    }

oc 方法

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 3);  //线宽
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetRGBStrokeColor(context, 0.0 / 255.0, 0.0 / 255.0, 0.0 / 0.0, 1.0);  //线的颜色
    CGContextBeginPath(context);
    
    CGContextMoveToPoint(context, 0, 0);  //起点坐标
    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);   //终点坐标
    
    CGContextStrokePath(context);
}

注: UItableView cell 上划线会被 contentView 背景遮住 需要让他得背景透明

有时候觉得这样去画一条线太麻烦不如,add个View 或者layer 啥的, 所以现在扩展UIView 写一个通用的只是添加一个线的函数

    /**
     UIView 扩展画线
     
     - parameter X:      X 线的X
     - parameter Y:      Y 线的X
     - parameter width:  width 线的width
     - parameter height: height 线的height
     - parameter coloer: coloer 线的颜色
     */
    func cz_AddBorderLine(X:CGFloat , Y:CGFloat ,width:CGFloat,height:CGFloat,coloer:UIColor)  {
        let border = CALayer()
        border.backgroundColor = coloer.CGColor
        border.frame = CGRect(x: X, y: Y, width: width, height: height)
        layer.addSublayer(border)
    }

标签: 暂无
最后更新:2022年 11月 11日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang