給UIView在xib中添加可視化的屬性
效果如下圖:
可以直接設(shè)置view 的 borderColor 哼勇、borderWidth、cornerRadius采蚀,也可以單獨(dú)指定view的某個(gè)角是圓角咪惠。減少了代碼中的屬性。
完整代碼:
UIView+Border.h
#import <UIKit/UIKit.h>
@interface UIView (Border)
/// 可以在xib里面直接設(shè)置的:邊線顏色
@property (nonatomic) IBInspectable UIColor *borderColor;
/// 可以在xib里面直接設(shè)置的:邊線寬度
@property (nonatomic) IBInspectable CGFloat borderWidth;
/// 可以在xib里面直接設(shè)置的:圓角
@property (nonatomic) IBInspectable CGFloat cornerRadius;
/// 可以在xib里面直接設(shè)置的:裁剪角的掩碼
@property (nonatomic) IBInspectable BOOL topLeft;
@property (nonatomic) IBInspectable BOOL topRight;
@property (nonatomic) IBInspectable BOOL bottomLeft;
@property (nonatomic) IBInspectable BOOL bottomRight;
@end
UIView+Border.m
#import "UIView+Border.h"
@implementation UIView (Border)
@dynamic borderColor, borderWidth, cornerRadius, topLeft, topRight, bottomLeft, bottomRight;
- (void)setBorderColor:(UIColor *)borderColor{
self.layer.borderColor = borderColor.CGColor;
}
- (void)setBorderWidth:(CGFloat)borderWidth{
self.layer.borderWidth = borderWidth;
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
self.layer.cornerRadius = cornerRadius;
}
- (void)setTopLeft:(BOOL)topLeft {
[self updateMaskedCornersFor:topLeft corner:kCALayerMinXMinYCorner];
}
- (void)setTopRight:(BOOL)topRight {
[self updateMaskedCornersFor:topRight corner:kCALayerMaxXMinYCorner];
}
- (void)setBottomLeft:(BOOL)bottomLeft {
[self updateMaskedCornersFor:bottomLeft corner:kCALayerMinXMaxYCorner];
}
- (void)setBottomRight:(BOOL)bottomRight {
[self updateMaskedCornersFor:bottomRight corner:kCALayerMaxXMaxYCorner];
}
- (void)updateMaskedCornersFor:(BOOL)shouldAdd corner:(CACornerMask)corner {
if (@available(iOS 11.0, *)) {
if (shouldAdd) {
self.layer.maskedCorners |= corner;
} else {
self.layer.maskedCorners &= ~corner;
}
}
}
@end
使用方法:
只需要把兩個(gè)類(lèi)倒入到項(xiàng)目中疏尿,xib中就會(huì)自動(dòng)出現(xiàn)上面截圖中的屬性瘟芝。