1 2 3 4 5 6 7 8 9 10 11 12 
  | 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 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 
  | - (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 写一个通用的只是添加一个线的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
  |  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) } 
  |