.h文件
#import <UIKit/UIKit.h>
@interface dashRoundCornerBtn : UIButton
@property(nonatomic ,retain)UIColor *lineColor;///<线的颜色 缺省:蓝色
@property(nonatomic ,assign)float lineWidth;///<线宽 缺省:1
@property(nonatomic ,assign)BOOL isDash;//是否为虚线 缺省:否
@property(nonatomic ,assign)float radius;///<圆角半径 缺省:10
@end
.m文件
#import "dashRoundCornerBtn.h"
@interface dashRoundCornerBtn ()
@property(nonatomic ,retain)UIColor *backColor;
@end
@implementation dashRoundCornerBtn
//懒加载
-(UIColor *)lineColor
{
if (!_lineColor)
{
self.lineColor = [UIColor blueColor];
}
return _lineColor;
}
-(float)lineWidth
{
if (!_lineWidth)
{
self.lineWidth = 1;
}
return _lineWidth;
}
-(BOOL)isDash
{
if (!_isDash)
{
self.isDash = NO;
}
return _isDash;
}
-(float)radius
{
if (!_radius)
{
self.radius = 10;
}
return _radius;
}
-(UIColor *)backgroundColor
{
if (!_backColor)
{
self.backColor = [UIColor clearColor];
}
return _backColor;
}
-(void)setBackgroundColor:(UIColor *)backgroundColor
{
_backColor = backgroundColor;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
float width = rect.size.width;
float height = rect.size.height;
float lineWidth = self.lineWidth;
float radius = self.radius;
CGContextRef context = UIGraphicsGetCurrentContext();
//设置线条样式
CGContextSetLineCap(context, kCGLineCapSquare);
//设置线条粗细宽度
CGContextSetLineWidth(context, lineWidth);
if (self.isDash)
{
CGFloat dashArray[] = {5*lineWidth};
CGContextSetLineDash(context, 0, dashArray, 1);//画虚线
}
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, (lineWidth+radius), lineWidth);
CGContextAddLineToPoint(context, width-(lineWidth+radius), lineWidth);
CGContextAddArc(context, width-(lineWidth+radius), (lineWidth+radius), radius, -M_PI_2, 0, 0);
CGContextAddLineToPoint(context, width-lineWidth, height-(lineWidth+radius));
CGContextAddArc(context, width-(lineWidth+radius), height-(lineWidth+radius), radius, 0, M_PI_2, 0);
CGContextAddLineToPoint(context, (lineWidth+radius), height-lineWidth);
CGContextAddArc(context, (lineWidth+radius), height-(lineWidth+radius), radius, M_PI_2, M_PI, 0);
CGContextAddLineToPoint(context, lineWidth, (lineWidth+radius));
CGContextAddArc(context, (lineWidth+radius), (lineWidth+radius),radius, M_PI, -M_PI_2, 0);
CGContextStrokePath(context);
//填充了一段路径:
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextMoveToPoint(context, (lineWidth+radius), lineWidth);
CGContextAddLineToPoint(context, width-(lineWidth+radius), lineWidth);
CGContextAddArc(context, width-(lineWidth+radius), (lineWidth+radius), radius, -M_PI_2, 0, 0);
CGContextAddLineToPoint(context, width-lineWidth, height-(lineWidth+radius));
CGContextAddArc(context, width-(lineWidth+radius), height-(lineWidth+radius), radius, 0, M_PI_2, 0);
CGContextAddLineToPoint(context, (lineWidth+radius), height-lineWidth);
CGContextAddArc(context, (lineWidth+radius), height-(lineWidth+radius), radius, M_PI_2, M_PI, 0);
CGContextAddLineToPoint(context, lineWidth, (lineWidth+radius));
CGContextAddArc(context, (lineWidth+radius), (lineWidth+radius),radius, M_PI, -M_PI_2, 0);
CGContextSetFillColorWithColor(context, self.backColor.CGColor);
CGContextFillPath(context);
}
@end
调用
#import "ViewController.h"
#import "dashRoundCornerBtn.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet dashRoundCornerBtn *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.btn.isDash = YES;
self.btn.lineWidth = 2;
self.btn.radius = 20;
// Do any additional setup after loading the view, typically from a nib.
}
@end