美文网首页
XIB 设置IB_DESIGNABLE 及IBInspectab

XIB 设置IB_DESIGNABLE 及IBInspectab

作者: fairy_tail_zc | 来源:发表于2018-09-05 18:30 被阅读0次

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

  • UIView+XIBLayer.h
#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
  • UIView+XIBLayer.m
#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
  • UIControl+XIBLayer.h
#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
  • UIControl+XIBLayer.m
#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

相关文章

网友评论

      本文标题:XIB 设置IB_DESIGNABLE 及IBInspectab

      本文链接:https://www.haomeiwen.com/subject/wjqzwftx.html