IB_DESIGNABLE
設(shè)置設(shè)置實時更新 XIB 中的關(guān)于 layer 的設(shè)置
IBInspectable
讓屬性設(shè)置可以在屬性面板上直接設(shè)置
-
IB_DESIGNABLE
IB_DESIGNABLE它寫在類的前面,如
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CustomView : UIView
@end
效果就是能再XIB上及時看到當(dāng)前類調(diào)改的效果,比如設(shè)置一個繪圖Color的時候或者設(shè)置圓角,可以立即呈現(xiàn).
-
IBInspectable
IBInspectable是設(shè)置屬性的,在屬性類型前面,如
@property (nonatomic,strong) IBInspectable UILabel *label;
如創(chuàng)建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