XIB中设置圆角等属性

开发过程中无可避免用到XIB,一些图片需要设置圆角之类的属性,但是XIB中并类似下面图片的中功能没有直接可以设置。这时候需要给view添加一个类别,然后在拓展这些属性进去


XIB中设置圆角等属性_第1张图片
屏幕快照 2017-05-24 下午10.16.16.png

给UIView添加HFIBnspectable类别,然后在.h文件中重新定义cornerRadius和borderWidth属性

#import 
@interface UIView (HFIBnspectable)
@property (nonatomic,assign)IBInspectable NSInteger cornerRadius;
@property (nonatomic,assign)IBInspectable NSInteger borderWidth;
@property (nonatomic,assign)IBInspectable BOOL masksToBounds;
@end

然后在.m文件中重写这两个属性的set方法和get方法

#import "UIView+HFIBnspectable.h"

@implementation UIView (HFIBnspectable)

#pragma mark - setCornerRadius/borderWidth

-(void)setCornerRadius:(NSInteger)cornerRadius
{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}
-(NSInteger)cornerRadius
{
    return self.layer.cornerRadius;
}
-(void)setBorderWidth:(NSInteger)borderWidth
{
    self.layer.borderWidth = borderWidth;
}

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


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

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

你可能感兴趣的:(XIB中设置圆角等属性)