XIB 设置IB_DESIGNABLE 及IBInspectab

2018-09-05  本文已影响0人  fairy_tail_zc

IB_DESIGNABLE
设置设置实时更新 XIB 中的关于 layer 的设置
IBInspectable
让属性设置可以在属性面板上直接设置

  1. IB_DESIGNABLE
    IB_DESIGNABLE它写在类的前面,如
#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface CustomView : UIView 

@end

效果就是能再XIB上及时看到当前类调改的效果,比如设置一个绘图Color的时候或者设置圆角,可以立即呈现.

  1. IBInspectable
    IBInspectable是设置属性的,在属性类型前面,如
@property (nonatomic,strong) IBInspectable UILabel *label;

如创建UIView的category扩展 可以在xib上直接修改cornerRadious 、masksToBounds、 borderColor、 borderWidth

#import <UIKit/UIKit.h>

@interface UIView (XIBLayer)

@property (nonatomic, assign) IBInspectable CGFloat cornerRadious;
@property (nonatomic, assign) IBInspectable BOOL masksToBounds;
@property (nonatomic, strong) IBInspectable UIColor* borderColor;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;


@end
#import "UIView+XIBLayer.h"

@implementation UIView (XIBLayer)
- (CGFloat)cornerRadious {
    return self.layer.cornerRadius;
}

- (void)setCornerRadious:(CGFloat)cornerRadious {
    self.layer.cornerRadius = cornerRadious;
}

- (BOOL)masksToBounds {
    return self.layer.masksToBounds;
}

- (void)setMasksToBounds:(BOOL)masksToBounds {
    self.layer.masksToBounds = masksToBounds;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}
- (CGFloat)boderWidth {
    return self.layer.borderWidth;
}

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

@end
#import <UIKit/UIKit.h>

@interface UIControl (XIBLayer)

@property (nonatomic, assign) IBInspectable CGFloat cornerRadious;
@property (nonatomic, assign) IBInspectable BOOL masksToBounds;
@property (nonatomic, strong) IBInspectable UIColor *borderColor;
@property (nonatomic, assign) IBInspectable CGFloat borderWidth;

@end
#import "UIControl+XIBLayer.h"

@implementation UIControl (XIBLayer)
- (CGFloat)cornerRadious {
    return self.layer.cornerRadius;
}

- (void)setCornerRadious:(CGFloat)cornerRadious {
    self.layer.cornerRadius = cornerRadious;
}

- (BOOL)masksToBounds {
    return self.layer.masksToBounds;
}

- (void)setMasksToBounds:(BOOL)masksToBounds {
    self.layer.masksToBounds = masksToBounds;
}

- (UIColor *)borderColor {
    return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setBorderColor:(UIColor *)borderColor {
    self.layer.borderColor = borderColor.CGColor;
}

-(CGFloat)borderWidth
{
    return self.layer.borderWidth;
}

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

@end
上一篇 下一篇

猜你喜欢

热点阅读