IBInspectable 和 IB_DESIGNABLE

IBInspectable storyboard 中直接修改边框颜色和边框宽度
IB_DESIGNABLE storyboard 中直接显示出圆角效果

效果图

storyboard 中直接修改边框颜色和边框宽度

storyboard 中直接显示出圆角效果

IBInspectable

用 IBInspectable 声明变量,变量会直接显示到 storyboard 中 属性检查器里面,会像修改 view 颜色一样方便的进行修改。

@property (nonatomic,strong) IBInspectable UIColor *borderC;
@property (nonatomic,assign) IBInspectable CGFloat borderWidth;

IB_DESIGNABLE

声明类,对类进行自定义,自定义效果会直接显示到 storyboard 中。

IB_DESIGNABLE

@interface BorderView : UIView

核心代码

BorderView.h
#import 


IB_DESIGNABLE

@interface BorderView : UIView

@property (nonatomic,strong) IBInspectable UIColor *borderC;

@property (nonatomic,assign) IBInspectable CGFloat borderWidth;


@end
BorderView.m
#import "BorderView.h"

@implementation BorderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 50;
    }
    return self;
}

- (void)setBorderC:(UIColor *)borderC
{
    _borderC = borderC;
    self.layer.borderColor = borderC.CGColor;
}

- (void)setBorderWidth:(CGFloat)borderWidth
{
    _borderWidth = borderWidth;
    self.layer.borderWidth = borderWidth;
}

@end

下载demo

你可能感兴趣的:(IBInspectable 和 IB_DESIGNABLE)