您的位置:首页 > 产品设计 > UI/UE

UIImage 虚线框 可以用作 UITextfield、UIButton 的background,

2016-03-11 23:36 417 查看
1.方法一、

//size是背景图尺寸,border color是边框颜色,border width是边框大小。

extension UIImage {

class public
func getImageWithLineBroken(size:CGSize,borderColor:UIColor,borderWidth:CGFloat) ->
UIImage {

UIGraphicsBeginImageContextWithOptions(size,
false,
0.0);

UIColor.clearColor().set()

let context =
UIGraphicsGetCurrentContext();

CGContextBeginPath(context);

CGContextSetLineWidth(context, borderWidth);

CGContextSetStrokeColorWithColor(context, borderColor.CGColor);

let lengths:[CGFloat] = [3,
1];

CGContextSetLineDash(context,
0, lengths, 1);

// line 1

CGContextMoveToPoint(context,
30.0, 0.0);

CGContextAddLineToPoint(context,
170,
0.0);

// line 2

CGContextAddArcToPoint(context,
200 , 30, 170,
60, 30)

// line 3

//CGContextAddLineToPoint(context, 200, 30);

// line 4

CGContextAddLineToPoint(context,
170,
60);

CGContextAddLineToPoint(context,
30,
60);

CGContextAddArcToPoint(context,
0 , 30, 30,
0, 30)

CGContextAddLineToPoint(context,
30,
0);

// CGContextAddArc(context, 170, 30, 30, CGFloat(M_PI * 270/180), CGFloat(M_PI_2), 0)

// //CGContextMoveToPoint(context, 170, 60);

// CGContextAddLineToPoint(context, 30, 60);

//

// CGContextAddArc(context, 30, 30, 30, CGFloat(M_PI_2), CGFloat(M_PI * 270/180), 0)

// CGContextMoveToPoint(context, 170, 60);

CGContextStrokePath(context);

let image =
UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: