#import "UIView+Border.h"
@implementation UIView (Border)
- (void)cornerRadius:(CGFloat)radius
topLeft:(BOOL)topLeft
topRight:(BOOL)topRight
bottomLeft:(BOOL)bottomLeft
bottomRight:(BOOL)bottomRight {
[self cornerRadius:radius inRect:self.bounds topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight];
}
// 自定义圆角, 指定圆角区域
- (void)cornerRadius:(CGFloat)radius
inRect:(CGRect)inRect
topLeft:(BOOL)topLeft
topRight:(BOOL)topRight
bottomLeft:(BOOL)bottomLeft
bottomRight:(BOOL)bottomRight {
if (@available(iOS 11.0, *))
{
CACornerMask cornerMask = 0;
if (topLeft) {
cornerMask |= kCALayerMinXMinYCorner;
}
if (topRight) {
cornerMask |= kCALayerMaxXMinYCorner;
}
if (bottomLeft) {
cornerMask |= kCALayerMinXMaxYCorner;
}
if (bottomRight) {
cornerMask |= kCALayerMaxXMaxYCorner;
}
self.layer.maskedCorners = cornerMask;
self.layer.cornerRadius = radius;
}
else
{
UIRectCorner corner = 0;
if (topLeft) {
corner |= UIRectCornerTopLeft;
}
if (topRight) {
corner |= UIRectCornerTopRight;
}
if (bottomLeft) {
corner |= UIRectCornerBottomLeft;
}
if (bottomRight) {
corner |= UIRectCornerBottomRight;
}
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = inRect;
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:inRect byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)].CGPath;
self.layer.mask = maskLayer;
}
}
// 自定义边框
- (void)borderColor:(UIColor *)color
width:(CGFloat)width
top:(BOOL)top
left:(BOOL)left
bottom:(BOOL)bottom
right:(BOOL)right {
if (top) {
[self addTopBorderWithColor:color andWidth:width];
}
if (left) {
[self addLeftBorderWithColor:color andWidth:width];
}
if (bottom) {
[self addBottomBorderWithColor:color andWidth:width];
}
if (right) {
[self addRightBorderWithColor:color andWidth:width];
}
}
// ----------------------------------
- (void)addTopBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *borderLayer = [self getBorderLayer:@"topBorder"];
borderLayer.frame = CGRectMake(0, 0, self.frame.size.width, borderWidth);
[self setBorder:borderLayer color:color width:borderWidth];
}
- (void)addBottomBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *borderLayer = [self getBorderLayer:@"bottomBorder"];
borderLayer.frame = CGRectMake(0, self.frame.size.height - borderWidth, self.frame.size.width, borderWidth);
[self setBorder:borderLayer color:color width:borderWidth];
}
- (void)addLeftBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *borderLayer = [self getBorderLayer:@"leftBorder"];
borderLayer.frame = CGRectMake(0, 0, borderWidth, self.frame.size.height);
[self setBorder:borderLayer color:color width:borderWidth];
}
- (void)addRightBorderWithColor:(UIColor *)color andWidth:(CGFloat) borderWidth
{
CALayer *borderLayer = [self getBorderLayer:@"rightBorder"];
borderLayer.frame = CGRectMake(self.frame.size.width - borderWidth, 0, borderWidth, self.frame.size.height);
[self setBorder:borderLayer color:color width:borderWidth];
}
static const char borderLayersKey = '\0';
- (NSMutableDictionary *)borderLayers {
NSMutableDictionary *dict = objc_getAssociatedObject(self, &borderLayersKey);
if (!dict) {
dict = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &borderLayersKey, dict, OBJC_ASSOCIATION_RETAIN);
}
return dict;
}
- (CALayer *)getBorderLayer:(NSString *)key {
NSMutableDictionary *borderLayers = [self borderLayers];
CALayer *layer = borderLayers[key];
if (!layer) {
layer = [CALayer layer];
borderLayers[key] = layer;
}
return layer;
}
- (void)setBorder:(CALayer *)borderLayer color:(UIColor *)color width:(CGFloat)width {
CGFloat alpha;
[color getRed:nil green:nil blue:nil alpha:&alpha];
if (alpha <= 0 || width <= 0) {
[borderLayer removeFromSuperlayer];
} else {
borderLayer.backgroundColor = color.CGColor;
[self.layer addSublayer:borderLayer];
}
}
@end
UIView自定义四个圆角和边框
最新推荐文章于 2024-08-28 21:01:31 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)