OC UIView分类使用IBInspectable,在xib上设置属性

代码链接 https://github.com/dkdsj/xibAdapter.git

1. 设置不规则圆角的方式

  • 设置cornerRadius
  • 设置统一的圆角allCornerRadius
  • 设置每个圆角: (上左、上右、下左、下右)

设置设置的时候注意⚠️优先级限制

 1. cornerRadiusOn
 = YES,cornerRadius生效,部分圆角设置无效
 = NO, cornerRadius无效,部分圆角设置生效

 2. 部分圆角
设置圆角同样的值 allCornerRadius
 > 0,allCornerRadius生效,单个圆角(上左、上右、下左、下右)设置无效
 <= 0,allCornerRadius无效,单个圆角(上左、上右、下左、下右)设置生效
圆角

分类
UIView+BInspectable.h

NS_ASSUME_NONNULL_BEGIN

/**
 优先级
 1. cornerRadiusOn
 = YES,cornerRadius生效,部分圆角设置无效
 = NO, cornerRadius无效,部分圆角设置生效

 2. 部分圆角
 allCornerRadius
 > 0,单个圆角(上左、上右、下左、下右)设置无效
 <= 0,单个圆角(上左、上右、下左、下右)设置生效
 */
@interface UIView (BInspectable)

///按比例缩放
//@property (nonatomic, assign) IBInspectable BOOL adapterRadius;
@property (nonatomic, assign) IBInspectable BOOL masks2Bounds;
@property (nonatomic, assign) IBInspectable BOOL cornerRadiusOn;
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;

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

@property (nonatomic, assign) IBInspectable CGFloat allCornerRadius;
///设置部分圆角
@property (nonatomic, assign) IBInspectable CGFloat topLeftCornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat topRightCornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat bottomLeftCornerRadius;
@property (nonatomic, assign) IBInspectable CGFloat bottomRightCornerRadius;

@end

NS_ASSUME_NONNULL_END

UIView+BInspectable.m

#import "UIView+BInspectable.h"
#import 

#pragma mark - 自适应
/**
 *  基准屏幕宽度 375.0
 *  以屏幕宽度为固定比例关系,来计算对应的值。假设:基准屏幕宽度375,floatV=10;当前屏幕宽度为750时,那么返回的值为20
 */
#define kAdaptW(floatValue) ({\
    float tmp = 0.0f;\
    if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {\
        tmp = floatValue * 1.5;\
    } else {\
        tmp = floatValue*[[UIScreen mainScreen] bounds].size.width/375.0;\
    }\
    tmp;\
})

#ifdef DEBUG
#define NSLog(format,...) printf("\n[%s] %s [第%d行] %s\n",__TIME__,__FUNCTION__,__LINE__,[[NSString stringWithFormat:format,## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif

@implementation UIView (BInspectable)

//radius按比例缩放
- (BOOL)adapterRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setAdapterRadius:(BOOL)adapterRadius {
    objc_setAssociatedObject(self, @selector(adapterRadius), @(adapterRadius), OBJC_ASSOCIATION_ASSIGN);
}

//圆角
- (CGFloat)cornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setCornerRadius:(CGFloat)cornerRadius {
    objc_setAssociatedObject(self, @selector(cornerRadius), @(cornerRadius), OBJC_ASSOCIATION_ASSIGN);
        
    if (self.cornerRadiusOn) {
        self.layer.cornerRadius = self.adapterRadius ? kAdaptW(cornerRadius) : cornerRadius;
    }
}

- (BOOL)cornerRadiusOn {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setCornerRadiusOn:(BOOL)cornerRadiusOn {
    objc_setAssociatedObject(self, @selector(cornerRadiusOn), @(cornerRadiusOn), OBJC_ASSOCIATION_ASSIGN);
    
    //会出现setCornerRadius时,这里还没有执行
    if (cornerRadiusOn) {
        self.cornerRadius = self.cornerRadius;
    }
}

- (BOOL)masks2Bounds {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setMasks2Bounds:(BOOL)masks2Bounds {
    objc_setAssociatedObject(self, @selector(masks2Bounds), @(masks2Bounds), OBJC_ASSOCIATION_ASSIGN);
    
    if (self.cornerRadiusOn) {
        self.layer.masksToBounds = masks2Bounds;
    }
}

//边框
- (CGFloat)borderWidth {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setBorderWidth:(CGFloat)borderWidth {
    objc_setAssociatedObject(self, @selector(borderWidth), @(borderWidth), OBJC_ASSOCIATION_ASSIGN);
    
    self.layer.borderWidth = borderWidth;
}

- (UIColor *)borderColor {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setBorderColor:(UIColor *)borderColor {
    objc_setAssociatedObject(self, @selector(borderColor), borderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (self.borderWidth > 0) {
        self.layer.borderColor = borderColor.CGColor;
    }
}


//部分圆角
- (CGFloat)allCornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setAllCornerRadius:(CGFloat)allCornerRadius {
    objc_setAssociatedObject(self, @selector(allCornerRadius), @(allCornerRadius), OBJC_ASSOCIATION_ASSIGN);
    
    if (!self.cornerRadiusOn && self.allCornerRadius > 0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(allCornerRadius, allCornerRadius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
    }
}

- (CGFloat)topLeftCornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setTopLeftCornerRadius:(CGFloat)topLeftCornerRadius {
    objc_setAssociatedObject(self, @selector(topLeftCornerRadius), @(topLeftCornerRadius), OBJC_ASSOCIATION_ASSIGN);
    
    [self updateSomeCorner];
}

- (CGFloat)topRightCornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setTopRightCornerRadius:(CGFloat)topRightCornerRadius {
    objc_setAssociatedObject(self, @selector(topRightCornerRadius), @(topRightCornerRadius), OBJC_ASSOCIATION_ASSIGN);
    
    [self updateSomeCorner];
}

- (CGFloat)bottomLeftCornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setBottomLeftCornerRadius:(CGFloat)bottomLeftCornerRadius {
    objc_setAssociatedObject(self, @selector(bottomLeftCornerRadius), @(bottomLeftCornerRadius), OBJC_ASSOCIATION_ASSIGN);
    
    [self updateSomeCorner];
}

- (CGFloat)bottomRightCornerRadius {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setBottomRightCornerRadius:(CGFloat)bottomRightCornerRadius {
    objc_setAssociatedObject(self, @selector(bottomRightCornerRadius), @(bottomRightCornerRadius), OBJC_ASSOCIATION_ASSIGN);

    [self updateSomeCorner];
}

- (void)updateSomeCorner {
    NSLog(@"%f %f %f %f %f", self.topLeftCornerRadius, self.topRightCornerRadius, self.bottomLeftCornerRadius, self.bottomRightCornerRadius, self.allCornerRadius);
    
    if (self.cornerRadiusOn || (self.allCornerRadius > 0)
        || (self.topLeftCornerRadius == 0
            && self.topRightCornerRadius == 0
            && self.bottomLeftCornerRadius == 0
            && self.bottomRightCornerRadius == 0)) {
        return;
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    CGPathRef path = CYPathCreateWithRoundedRect(self.bounds, self.topLeftCornerRadius, self.topRightCornerRadius, self.bottomLeftCornerRadius, self.bottomRightCornerRadius);
    shapeLayer.path = path;
    CGPathRelease(path);
    self.layer.mask = shapeLayer;
}

/**
 切圆角函数
 出自 https://github.com/MrGCY/AnyCornerRadius
 https://www.jianshu.com/p/164106443353
 */
CGPathRef CYPathCreateWithRoundedRect(CGRect bounds, CGFloat topLeft, CGFloat topRight, CGFloat bottomLeft, CGFloat bottomRight) {
     const CGFloat minX = CGRectGetMinX(bounds);
     const CGFloat minY = CGRectGetMinY(bounds);
     const CGFloat maxX = CGRectGetMaxX(bounds);
     const CGFloat maxY = CGRectGetMaxY(bounds);
     
     const CGFloat topLeftCenterX = minX +  topLeft;
     const CGFloat topLeftCenterY = minY + topLeft;
     
     const CGFloat topRightCenterX = maxX - topRight;
     const CGFloat topRightCenterY = minY + topRight;
     
     const CGFloat bottomLeftCenterX = minX +  bottomLeft;
     const CGFloat bottomLeftCenterY = maxY - bottomLeft;
     
     const CGFloat bottomRightCenterX = maxX -  bottomRight;
     const CGFloat bottomRightCenterY = maxY - bottomRight;
     /*
      path : 路径
      m : 变换
      x  y : 画圆的圆心点
      radius : 圆的半径
      startAngle : 起始角度
      endAngle : 结束角度
      clockwise : 是否是顺时针
      void CGPathAddArc(CGMutablePathRef cg_nullable path,
      const CGAffineTransform * __nullable m,
      CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle,
      bool clockwise)
      */
     //虽然顺时针参数是YES,在iOS中的UIView中,这里实际是逆时针
     
     CGMutablePathRef path = CGPathCreateMutable();
     //顶 左
     CGPathAddArc(path, NULL, topLeftCenterX, topLeftCenterY,topLeft, M_PI, 3 * M_PI_2, NO);
     //顶 右
     CGPathAddArc(path, NULL, topRightCenterX , topRightCenterY, topRight, 3 * M_PI_2, 0, NO);
     //底 右
     CGPathAddArc(path, NULL, bottomRightCenterX, bottomRightCenterY, bottomRight,0, M_PI_2, NO);
     //底 左
     CGPathAddArc(path, NULL, bottomLeftCenterX, bottomLeftCenterY, bottomLeft, M_PI_2,M_PI, NO);
     CGPathCloseSubpath(path);
     return path;
}

@end

2. 设置UILabel、UIButton、UITextField、UITextView的字体大小

添加的这些属性可以按需添加

按照这个顺序,最后设置的(size > 0)生效,可按需添加
 UIKIT_EXTERN const UIFontWeight UIFontWeightUltraLight;
 UIKIT_EXTERN const UIFontWeight UIFontWeightThin;
 UIKIT_EXTERN const UIFontWeight UIFontWeightLight;
 UIKIT_EXTERN const UIFontWeight UIFontWeightRegular;
 UIKIT_EXTERN const UIFontWeight UIFontWeightMedium;
 UIKIT_EXTERN const UIFontWeight UIFontWeightSemibold;
 UIKIT_EXTERN const UIFontWeight UIFontWeightBold;
 UIKIT_EXTERN const UIFontWeight UIFontWeightHeavy;
 UIKIT_EXTERN const UIFontWeight UIFontWeightBlack;

@property (nonatomic, assign) IBInspectable CGFloat fixUltraLightFont;
@property (nonatomic, assign) IBInspectable CGFloat fixThinFont;
@property (nonatomic, assign) IBInspectable CGFloat fixLightFont;
@property (nonatomic, assign) IBInspectable CGFloat fixRegularFont;
@property (nonatomic, assign) IBInspectable CGFloat fixMediumFont;
@property (nonatomic, assign) IBInspectable CGFloat fixSemiboldFont;
@property (nonatomic, assign) IBInspectable CGFloat fixBoldFont;
@property (nonatomic, assign) IBInspectable CGFloat fixHeavyFont;
@property (nonatomic, assign) IBInspectable CGFloat fixBlackFont;
image.png

分类
UILabel+BInspectable.h

#import 

NS_ASSUME_NONNULL_BEGIN

/**
 按照这个顺序,最后设置的(size > 0)生效,可按需添加
 UIKIT_EXTERN const UIFontWeight UIFontWeightUltraLight;
 UIKIT_EXTERN const UIFontWeight UIFontWeightThin;
 UIKIT_EXTERN const UIFontWeight UIFontWeightLight;
 UIKIT_EXTERN const UIFontWeight UIFontWeightRegular;
 UIKIT_EXTERN const UIFontWeight UIFontWeightMedium;
 UIKIT_EXTERN const UIFontWeight UIFontWeightSemibold;
 UIKIT_EXTERN const UIFontWeight UIFontWeightBold;
 UIKIT_EXTERN const UIFontWeight UIFontWeightHeavy;
 UIKIT_EXTERN const UIFontWeight UIFontWeightBlack;
 */
@interface UILabel (BInspectable)

@property (nonatomic, assign) IBInspectable CGFloat fixUltraLightFont;
@property (nonatomic, assign) IBInspectable CGFloat fixThinFont;
@property (nonatomic, assign) IBInspectable CGFloat fixLightFont;
@property (nonatomic, assign) IBInspectable CGFloat fixRegularFont;
@property (nonatomic, assign) IBInspectable CGFloat fixMediumFont;
@property (nonatomic, assign) IBInspectable CGFloat fixSemiboldFont;
@property (nonatomic, assign) IBInspectable CGFloat fixBoldFont;
@property (nonatomic, assign) IBInspectable CGFloat fixHeavyFont;
@property (nonatomic, assign) IBInspectable CGFloat fixBlackFont;

@end

NS_ASSUME_NONNULL_END

UILabel+BInspectable.m

#import "UILabel+BInspectable.h"
#import 

#pragma mark - 自适应
/**
 *  基准屏幕宽度 375.0
 *  以屏幕宽度为固定比例关系,来计算对应的值。假设:基准屏幕宽度375,floatV=10;当前屏幕宽度为750时,那么返回的值为20
 */
#define kAdaptW(floatValue) ({\
    float tmp = 0.0f;\
    if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {\
        tmp = floatValue * 1.5;\
    } else {\
        tmp = floatValue*[[UIScreen mainScreen] bounds].size.width/375.0;\
    }\
    tmp;\
})

#ifdef DEBUG
#define NSLog(format,...) printf("\n[%s] %s [第%d行] %s\n",__TIME__,__FUNCTION__,__LINE__,[[NSString stringWithFormat:format,## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif

@implementation UILabel (BInspectable)

- (CGFloat)fixUltraLightFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixUltraLightFont:(CGFloat)fixUltraLightFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixUltraLightFont), @(fixUltraLightFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixUltraLightFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixUltraLightFont) weight:UIFontWeightUltraLight];
}

- (CGFloat)fixThinFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixThinFont:(CGFloat)fixThinFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixThinFont), @(fixThinFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixThinFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixThinFont) weight:UIFontWeightThin];
}

- (CGFloat)fixLightFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixLightFont:(CGFloat)fixLightFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixLightFont), @(fixLightFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixLightFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixLightFont) weight:UIFontWeightLight];
}

- (CGFloat)fixRegularFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixRegularFont:(CGFloat)fixRegularFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixRegularFont), @(fixRegularFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixRegularFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixRegularFont) weight:UIFontWeightRegular];
}

- (CGFloat)fixMediumFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixMediumFont:(CGFloat)fixMediumFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixMediumFont), @(fixMediumFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixMediumFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixMediumFont) weight:UIFontWeightMedium];
}

- (CGFloat)fixSemiboldFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixSemiboldFont:(CGFloat)fixSemiboldFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixSemiboldFont), @(fixSemiboldFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixSemiboldFont) {
        return;
    }
    
    self.font = [UIFont systemFontOfSize:kAdaptW(fixSemiboldFont) weight:UIFontWeightSemibold];
}

- (CGFloat)fixBoldFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixBoldFont:(CGFloat)fixBoldFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixBoldFont), @(fixBoldFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixBoldFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixBoldFont) weight:UIFontWeightBold];
}

- (CGFloat)fixHeavyFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixHeavyFont:(CGFloat)fixHeavyFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixHeavyFont), @(fixHeavyFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixHeavyFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixHeavyFont) weight:UIFontWeightHeavy];
}

- (CGFloat)fixBlackFont {
    //NSLog(@"%@", self);
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}
- (void)setFixBlackFont:(CGFloat)fixBlackFont {
    //NSLog(@"%@", self);
    objc_setAssociatedObject(self, @selector(fixBlackFont), @(fixBlackFont), OBJC_ASSOCIATION_ASSIGN);
    
    if (0 >= fixBlackFont) {
        return;
    }

    self.font = [UIFont systemFontOfSize:kAdaptW(fixBlackFont) weight:UIFontWeightBlack];
}

@end

下面的分类代码一样,UIButton设置的是self.titleLabel.font
UITextField+BInspectable.h
UITextView+BInspectable.h
UIButton+BInspectable.h

3. 设置约束

增加了adapterScreen开关,xib可以设置某约束是否按比例缩放

3个同样大小的view,中间的view设置width、height,left约束的adapteScren=YES,按比例拉伸了,效果如图

image.png

分类

NSLayoutConstraint+BInspectable.h



NS_ASSUME_NONNULL_BEGIN

@interface NSLayoutConstraint (BInspectable)
@property (nonatomic, assign) IBInspectable BOOL adapterScreen;
@end

NS_ASSUME_NONNULL_END

NSLayoutConstraint+BInspectable.m

#import "NSLayoutConstraint+BInspectable.h"
#import 

#pragma mark - 自适应
/**
 *  基准屏幕宽度 375.0
 *  以屏幕宽度为固定比例关系,来计算对应的值。假设:基准屏幕宽度375,floatV=10;当前屏幕宽度为750时,那么返回的值为20
 */
#define kAdaptW(floatValue) ({\
    float tmp = 0.0f;\
    if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {\
        tmp = floatValue * 1.5;\
    } else {\
        tmp = floatValue*[[UIScreen mainScreen] bounds].size.width/375.0;\
    }\
    tmp;\
})

@implementation NSLayoutConstraint (BInspectable)

- (BOOL)adapterScreen {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

- (void)setAdapterScreen:(BOOL)adapterScreen {
    objc_setAssociatedObject(self, @selector(adapterScreen), @(adapterScreen), OBJC_ASSOCIATION_ASSIGN);

    if (adapterScreen) {
        self.constant = kAdaptW(self.constant);
    }
}
@end

你可能感兴趣的:(OC UIView分类使用IBInspectable,在xib上设置属性)