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

相关推荐
吴Wu涛涛涛涛涛Tao15 小时前
基于TCA构建Instagram克隆:SwiftUI状态管理的艺术
ios·swiftui
2501_915921432 天前
iOS 应用上架多环境实战,Windows、Linux 与 Mac 的不同路径
android·ios·小程序·https·uni-app·iphone·webview
Cyclic10012 天前
IOS购买订阅通知信息解析说明Java
java·开发语言·ios
00后程序员张2 天前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
2501_916007473 天前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
大熊猫侯佩3 天前
拒绝羡慕 Cursor!Xcode 自己也能利用 AI 大模型让撸码如虎添翼【超详细配置】
macos·ai编程·xcode
ToTensor3 天前
Paraformer实时语音识别中的碎碎念
人工智能·语音识别·xcode
wjm0410063 天前
ios八股文 -- Objective-c
开发语言·ios·objective-c
麦兜*3 天前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
Digitally3 天前
重置iPhone会删除所有内容吗? 详细回答
ios·iphone