iOS控件设置虚线框

通过CAShapeLayer和UIBezierPath给控件添加虚线或设置虚线条。备用

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createShapeLayer_A];

   [self createShapeLayerLine];

    [self createShapeLayer_B];

}

//侧边的虚线:左和右。

- (void)createShapeLayer_A{

    UILabel *typeLabel = [self createLabelFrame:CGRectMake(100, 200,100, 80) TextContent: @"ShapeLayer_A" Font:[UIFont systemFontOfSize:15] BackgroundColor:[UIColor cyanColor] TextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:typeLabel];

    CAShapeLayer *shapeLayerLeft = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:[UIColor whiteColor]

                                                             BezierPathWithRect:CGRectMake(0, 0, 3, 80)

                                                             LineWidth:3.0

                                                             LineDashPattern:@[@1,@3]

                                                             BorderFrame:typeLabel.bounds];

    [typeLabel.layer addSublayer:shapeLayerLeft];

    CAShapeLayer *shapeLayerRight = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:[UIColor whiteColor]

                                                    BezierPathWithRect:CGRectMake(97, 0,3, 80)

                                                             LineWidth:3.0

                                                       LineDashPattern:@[@1,@3]

                                                           BorderFrame:typeLabel.bounds];

    [typeLabel.layer addSublayer:shapeLayerRight];

    typeLabel.layer.masksToBounds = YES;//修剪一下

}

//设置虚线条

- (void)createShapeLayerLine{

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 290, [UIScreen mainScreen].bounds.size.width, 1)];

    lineView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:lineView];

    CAShapeLayer *shape = [self hs_ShapeLayerMakerWithStrokeColor:[UIColor lightGrayColor] FillColor:[UIColor whiteColor] BezierPathWithRect:CGRectMake(0, 0, lineView.frame.size.width, 1) LineWidth:2 LineDashPattern:@[@2,@3] BorderFrame:lineView.bounds];

    [lineView.layer addSublayer:shape];

    lineView.layer.masksToBounds = YES;

}

/**

返回CAShapeLayer对象

@param strokeColor 虚线的颜色

@param fillColor 虚线间填充的颜色

@param bezierRect 设置虚线路径,决定了虚线存在于哪个位置

@param lineWidth 虚线宽度

@param lineDashPatter @[@a,@b] a:虚线高度,b:两虚线间隔高度

@param frame shapeLayer对象的frame

@return 返回CAShapeLayer对象从而添加到指定控件

*/

- (CAShapeLayer *)hs_ShapeLayerMakerWithStrokeColor:(UIColor *)strokeColor FillColor:(UIColor *)fillColor BezierPathWithRect:(CGRect)bezierRect LineWidth:(CGFloat)lineWidth LineDashPattern:(NSArray*)lineDashPatter BorderFrame:(CGRect)frame{

    CAShapeLayer *border = [CAShapeLayer layer];

    border.strokeColor = strokeColor.CGColor;

    border.fillColor = fillColor.CGColor;

    border.path = [UIBezierPath bezierPathWithRect:bezierRect].CGPath;

    border.lineWidth = lineWidth;//虚线伸出去的宽度

    border.lineDashPattern = lineDashPatter;//虚线高度 & 两虚线间隔高度

    border.frame = frame;

    return border;

}

//控件整个虚线框

- (void)createShapeLayer_B{

    UILabel *typeLabel = [self createLabelFrame:CGRectMake(100, 300,100, 80) TextContent: @"ShapeLayer_B" Font:[UIFont systemFontOfSize:15] BackgroundColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:typeLabel];

    

    CAShapeLayer *shapeLayer = [self hs_AllRoundedShapeLayerWithStrokeColor:[UIColor cyanColor]

                                                             FillColor:nil

                                             BezierPathWithRoundedRect:typeLabel.bounds

                                                           CornerRadius:5

                                                             LineWidth:1.0

                                                       LineDashPattern:@[@4,@2]

                                                           BorderFrame:typeLabel.bounds];

    

    typeLabel.layer.cornerRadius = 5;

    typeLabel.layer.masksToBounds = YES;//修饰一下

    [typeLabel.layer addSublayer:shapeLayer];

}

/**

控件四周都是虚线

@param strokeColor 虚线的颜色

@param fillColor 虚线间填充的颜色

@param bezierRect 设置虚线路径

@param cornerRadius 圆角大小

@param lineWidth 虚线伸出去的宽度

@param lineDashPatter @[@a,@b] a:虚线高度,b:两虚线间隔高度

@param frame shapeLayer对象的frame

@return 返回CAShapeLayer对象从而添加到指定控件

*/

- (CAShapeLayer *)hs_AllRoundedShapeLayerWithStrokeColor:(UIColor *)strokeColor FillColor:(UIColor *)fillColor BezierPathWithRoundedRect:(CGRect)bezierRect CornerRadius:(CGFloat)cornerRadius LineWidth:(CGFloat)lineWidth LineDashPattern:(NSArray*)lineDashPatter BorderFrame:(CGRect)frame{

    CAShapeLayer *border = [CAShapeLayer layer];

    border.strokeColor = strokeColor.CGColor;

    border.fillColor = fillColor.CGColor;

    border.path = [UIBezierPath bezierPathWithRoundedRect:bezierRect cornerRadius:cornerRadius].CGPath;

    border.lineWidth = lineWidth;

    border.lineDashPattern = lineDashPatter;

    border.frame = frame;

    return border;

}

//创建Label对象

- (UILabel *)createLabelFrame:(CGRect)labelFrame TextContent:(NSString *)text Font:(UIFont*)font BackgroundColor:(UIColor *)backgroundColor TextAlignment:(NSTextAlignment)textAlignment{

    UILabel*typeLabel = [[UILabel alloc]initWithFrame:labelFrame];

    typeLabel.text = text;

    typeLabel.numberOfLines = 0;

    typeLabel.font = font;

    typeLabel.backgroundColor = backgroundColor;

    typeLabel.textAlignment = textAlignment;

    return typeLabel;

}

猜你喜欢

转载自blog.csdn.net/SharkToping/article/details/81630753
今日推荐