You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.5 KiB
75 lines
2.5 KiB
// |
|
// UIView+Tools.m |
|
// tongxin |
|
// |
|
// Created by Apple on 2020/4/8. |
|
// Copyright © 2020 xTT. All rights reserved. |
|
// |
|
|
|
#import "UIView+Tools.h" |
|
|
|
|
|
@implementation UIView (Tools) |
|
|
|
- (void)setBorderWithCornerRadius:(CGFloat)cornerRadius |
|
borderWidth:(CGFloat)borderWidth |
|
borderColor:(UIColor *)borderColor |
|
type:(UIRectCorner)corners { |
|
|
|
// UIRectCorner type = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft; |
|
|
|
//1. 加一个layer 显示形状 |
|
CGRect rect = CGRectMake(borderWidth/2.0, borderWidth/2.0, |
|
CGRectGetWidth(self.frame)-borderWidth, CGRectGetHeight(self.frame)-borderWidth); |
|
CGSize radii = CGSizeMake(cornerRadius, borderWidth); |
|
|
|
//create path |
|
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii]; |
|
|
|
//create shape layer |
|
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; |
|
shapeLayer.strokeColor = borderColor.CGColor; |
|
shapeLayer.fillColor = [UIColor clearColor].CGColor; |
|
|
|
shapeLayer.lineWidth = borderWidth; |
|
shapeLayer.lineJoin = kCALineJoinRound; |
|
shapeLayer.lineCap = kCALineCapRound; |
|
shapeLayer.path = path.CGPath; |
|
|
|
[self.layer addSublayer:shapeLayer]; |
|
|
|
|
|
|
|
|
|
//2. 加一个layer 按形状 把外面的减去 |
|
CGRect clipRect = CGRectMake(0, 0, |
|
CGRectGetWidth(self.frame)-1, CGRectGetHeight(self.frame)-1); |
|
CGSize clipRadii = CGSizeMake(cornerRadius, borderWidth); |
|
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:clipRect byRoundingCorners:corners cornerRadii:clipRadii]; |
|
|
|
CAShapeLayer *clipLayer = [CAShapeLayer layer]; |
|
clipLayer.strokeColor = borderColor.CGColor; |
|
shapeLayer.fillColor = [UIColor clearColor].CGColor; |
|
|
|
clipLayer.lineWidth = 1; |
|
clipLayer.lineJoin = kCALineJoinRound; |
|
clipLayer.lineCap = kCALineCapRound; |
|
clipLayer.path = clipPath.CGPath; |
|
|
|
self.layer.mask = clipLayer; |
|
} |
|
|
|
/// 设置圆角 |
|
/// @param cornerRadius 半径 |
|
- (void)setCornerRadius:(CGFloat)cornerRadius{ |
|
self.layer.cornerRadius = cornerRadius; |
|
self.layer.masksToBounds = YES; |
|
} |
|
|
|
- (void)setCornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor{ |
|
self.layer.borderWidth = borderWidth; |
|
self.layer.borderColor = borderColor.CGColor; |
|
[self setCornerRadius:cornerRadius]; |
|
} |
|
|
|
@end
|
|
|