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

相关推荐
linweidong3 小时前
实战救火型 从 500MB 降到 50MB:高频业务场景下的 iOS 内存急救与避坑指南
macos·ios·objective-c·cocoa·ios面试·nstimer·ios面经
山里看瓜4 小时前
解决 iOS 上 Swiper 滑动图片闪烁问题:原因分析与最有效的修复方式
前端·css·ios
网络研究院4 小时前
苹果修复了iOS系统中两个被定向攻击利用的零日漏洞
macos·ios·cocoa
如此风景6 小时前
IOS SwiftUI 全组件详解
ios
雾散声声慢6 小时前
解决 iOS 上 Swiper 滑动图片闪烁问题:原因分析与最有效的修复方式
前端·css·ios
QuantumLeap丶8 小时前
《Flutter全栈开发实战指南:从零到高级》- 24 -集成推送通知
android·flutter·ios
YungFan8 小时前
iOS开发之MetricKit监控App性能
ios·swiftui·swift
二流小码农9 小时前
鸿蒙开发:上架困难?谈谈我的上架之路
android·ios·harmonyos
图图大恼9 小时前
在iOS上体验Open-AutoGLM:从安装到流畅操作的完整指南
人工智能·ios·agent
笑尘pyrotechnic11 小时前
[iOS原理] Block的本质
ios·objective-c·cocoa