大号在练葵花宝典 发表于 2024-6-14 23:09:13

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

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

效果如下图:
https://img-blog.csdnimg.cn/direct/94e4c757590e49f19a83c2afd6c4a458.png
可以直接设置view 的 borderColor 、borderWidth、cornerRadius,也可以单独指定view的某个角是圆角。减少了代码中的属性。
完整代码:

UIView+Border.h
#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
#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 {
    ;
}

- (void)setTopRight:(BOOL)topRight {
    ;
}

- (void)setBottomLeft:(BOOL)bottomLeft {
    ;
}

- (void)setBottomRight:(BOOL)bottomRight {
    ;
}

- (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中就会自动出现上面截图中的属性。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Xcode中给UIView在xib中添加可视化的属性