Xcode中給UIView在xib中添加可視化的屬性

給UIView在xib中添加可視化的屬性

效果如下圖:


xcode-xib.png

可以直接設(shè)置view 的 borderColor 、borderWidth、cornerRadius,也可以單獨指定view的某個角是圓角。減少了代碼中的屬性。

完整代碼:

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

使用方法:

只需要把兩個類倒入到項目中,xib中就會自動出現(xiàn)上面截圖中的屬性。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容