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)
}
文章评论