swift画线

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)
}
Author

陈昭

Posted on

2016-09-11

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.