xib可視化類別擴(kuò)展

不需要一句代碼,給UI控件設(shè)置圓角和陰影及1px的兼容
  • 在看這篇文章之前 請(qǐng)先參看IBInspectable 和 IB_DESIGNABLE
  • 在Xib中 系統(tǒng)自帶的UI控件中 添加一些 額外的屬性
  • eg:


  • 效果


示例代碼:

特別注意:
如果想實(shí)時(shí)監(jiān)聽MBIBnspectable的改變的話,需要繼承一個(gè)空的基類,或者自定義的類。
如果需要的話請(qǐng)繼承于PXBaseView.h

  • UIView的xib擴(kuò)展
  • .h文件
#import <UIKit/UIKit.h>
IB_DESIGNABLE
/** View的XIB可視化擴(kuò)展類> */
@interface UIView (IBnspectable)
/*!
 * 給UIView 設(shè)置圓角
 */
@property (assign,nonatomic) IBInspectable NSInteger cornerRadius;
@property (assign,nonatomic) IBInspectable BOOL  masksToBounds;

/*!
 * 設(shè)置 view的 邊框顏色(選擇器和Hex顏色) 
 * 以及 邊框的寬度
 */
@property (assign,nonatomic) IBInspectable NSInteger borderWidth;
@property (strong,nonatomic) IBInspectable NSString  *borderHexRgb;
@property (strong,nonatomic) IBInspectable UIColor   *borderColor;
/*!
 * 設(shè)置view的背景Hex顏色 (選擇器顏色 是系統(tǒng)自帶的不需要寫)
 */
@property (assign,nonatomic) IBInspectable NSString  *hexRgbColor;
//TODO: 特別注意
/*!
 * 這個(gè)屬性可以開啟 Retina屏對(duì) 1px的支持
 * Retain屏的分辨率 [UIScreen mainScreen].scale分辨率是 >=2
 * 
 */
@property (assign,nonatomic) IBInspectable BOOL      onePx;
@end
  • .m文件的實(shí)現(xiàn)
@implementation UIView (IBnspectable)
- (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;
}

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

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

- (void)setBorderHexRgb:(NSString *)borderHexRgb{
    NSScanner *scanner = [NSScanner scannerWithString:borderHexRgb];
    unsigned hexNum;
    //這里是將16進(jìn)制轉(zhuǎn)化為10進(jìn)制
    if (![scanner scanHexInt:&hexNum])
        return;
    self.layer.borderColor = [self colorWithRGBHex:hexNum].CGColor;
}

-(NSString *)borderHexRgb{
    return @"0xffffff";
}

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

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

#pragma mark - hexRgbColor
- (void)setHexRgbColor:(NSString *)hexRgbColor{
    NSScanner *scanner = [NSScanner scannerWithString:hexRgbColor];
    unsigned hexNum;
    if (![scanner scanHexInt:&hexNum]) return;
    self.backgroundColor = [self colorWithRGBHex:hexNum];
}

- (UIColor *)colorWithRGBHex:(UInt32)hex {
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}


- (NSString *)hexRgbColor{
    return @"0xffffff";
}

#pragma mark - setOnePx
- (void)setOnePx:(BOOL)onePx{
    if (onePx) {
        CGRect rect = self.frame;
        rect.size.height = 5/ [UIScreen mainScreen].scale;
        self.frame = rect;
    }
}

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

GitHub地址:https://github.com/421696067/Category-Xib.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,008評(píng)論 25 709
  • 引言 學(xué)到這里,xib給我?guī)淼膸椭呀?jīng)很大了,最大的莫過于UI控件的創(chuàng)建、屬性的賦值再也不用寫代碼,就UI開發(fā)來...
    二亮子閱讀 7,390評(píng)論 41 82
  • 2016年的考研大幕已經(jīng)拉下,2017年的考研大軍已經(jīng)滾滾而來。在網(wǎng)上充斥著各種考研成功的經(jīng)驗(yàn)貼,但是我很少看見考...
    姚小幺閱讀 1,273評(píng)論 5 3
  • 20170406學(xué)習(xí)筆記 (1)Chinese sci-fic writer nominated for seco...
    Joan一憶稀薄涼閱讀 143評(píng)論 0 0
  • 1/ 公交車上 L君戴著一只黑色的口罩,雙手插進(jìn)大衣的口袋里,將自己包裹得像粽子一樣嚴(yán)實(shí)——活生生一個(gè)重度感冒患者...
    上好的雞湯閱讀 333評(píng)論 0 0

友情鏈接更多精彩內(nèi)容