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

相关推荐
ZZH_AI项目交付29 分钟前
我把 AI 最容易改坏真实 App 的地方,整理成了 skills
人工智能·ios·app
00后程序员张1 小时前
Windows 下怎么生成 AppStoreInfo.plist?不依赖 Xcode 的方法
ide·macos·ios·小程序·uni-app·iphone·xcode
原鸣清1 小时前
iOS 自定义 Markdown 渲染实践:从成品库到可魔改 Demo
ios
Daniel_Coder2 小时前
iOS Widget 开发-18:Widget 的 SwiftUI 视图适配与设计
ios·swiftui·swift·widget·widgetcenter
Daniel_Coder2 小时前
iOS Widget 开发-17:Widget 错误处理与空状态设计
ios·swift·widget·widgetcenter
wjm0410062 小时前
简单谈谈ios开发中的UI
开发语言·ios·swift
恋猫de小郭4 小时前
Flutter 3.44 发布啦,超级大版本更新!!!
android·flutter·ios
天天开发4 小时前
Flutter开发者该掌握的iOS隐私审核政策
flutter·ios·cocoa
AGoodrMe18 小时前
swift基础之async/await
前端·ios
hhb_61818 小时前
Swift核心技术难点与实战案例解析
开发语言·ios·swift