Xcode中给UIView在xib中添加可视化的属性

给UIView在xib中添加可视化的属性

效果如下图:

可以直接设置view 的 borderColor 、borderWidth、cornerRadius,也可以单独指定view的某个角是圆角。减少了代码中的属性。

完整代码:

UIView+Border.h

objectivec 复制代码
#import <UIKit/UIKit.h>

@interface UIView (Border)

/// 可以在xib里面直接设置的:边线颜色
@property (nonatomic) IBInspectable UIColor *borderColor;

/// 可以在xib里面直接设置的:边线宽度
@property (nonatomic) IBInspectable CGFloat borderWidth;

/// 可以在xib里面直接设置的:圆角
@property (nonatomic) IBInspectable CGFloat cornerRadius;

/// 可以在xib里面直接设置的:裁剪角
@property (nonatomic) IBInspectable BOOL topLeft;
@property (nonatomic) IBInspectable BOOL topRight;
@property (nonatomic) IBInspectable BOOL bottomLeft;
@property (nonatomic) IBInspectable BOOL bottomRight;

@end

UIView+Border.m

objectivec 复制代码
#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中就会自动出现上面截图中的属性。

相关推荐
初级代码游戏13 小时前
Maui劝退:用windows直接真机调试iOS,无须和Mac配对
macos·ios·配置·maui·热重载
fundroid21 小时前
Swift 进军 Android,Kotlin 该如何应对?
android·ios
形影相吊1 天前
iOS防截屏实战
ios
吴Wu涛涛涛涛涛Tao1 天前
Flutter 弹窗解析:从系统 Dialog 到完全自定义
flutter·ios
kymjs张涛1 天前
零一开源|前沿技术周报 #7
android·前端·ios
思考着亮1 天前
15-错误处理
ios
思考着亮1 天前
9.方法
ios
思考着亮1 天前
6.结构体和类
ios
思考着亮1 天前
7.闭包
ios
咕噜签名分发冰淇淋1 天前
申请注册苹果iOS企业级开发者证书需要公司拥有什么规模条件
macos·ios·cocoa