OC UIButton 分类 链式设置不同state的样式

效果

selected
normal

下载 demo点这里 希望对你有点帮助👌👌

调用

UIButton *button = [UIButton new];
    [self.view addSubview:button];
    button.center = self.view.center;
    button.bounds = CGRectMake(0, 0, 200, 200);
  1. 设置normal
[button setUpStyle:UIControlStateNormal style:^(UIButton *button) {
        button
        .setUpTitle(@"normal")
        .setUpTitleColor([UIColor blueColor])
        .setUpBorderWidth(5)
        .setUpBorderColor([UIColor blueColor])
        .setUpBackgroundColor([UIColor grayColor])
        .setUpFont([UIFont systemFontOfSize:32])
        .setUpCornerRadius(12);
    }];
  1. 设置selected状态
[button setUpStyle:UIControlStateSelected style:^(UIButton *button) {
        button
        .setUpTitle(@"selected")
        .setUpTitleColor([UIColor redColor])
        .setUpBorderWidth(11)
        .setUpBorderColor([UIColor redColor])
        .setUpBackgroundColor([UIColor lightGrayColor])
        .setUpFont([UIFont systemFontOfSize:20])
        .setUpCornerRadius(100);
    }];
  1. 刷新button的样式
    adjustButtonStyleWithState:state
- (void)click: (UIButton *)button {
    button.selected = !button.selected;
    UIControlState state = button.selected ? UIControlStateSelected : UIControlStateNormal;
    [button adjustButtonStyleWithState:state];
}

思路

1. 注意

非线程安全 需要在主线程调用
- (void)setUpStyle:(UIControlState) state style:(void(^)(UIButton *button))callBack

2. 实现

  1. 内部添加一个属性记录 setUpStyle:style: 传入的style
  2. 添加方法 字典,储存相应的状态下的相关样式
@property (nonatomic,assign) UIControlState setupState;

@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIColor *>*backgroundColorDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIColor *>*borderColorDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,NSNumber *>*borderWidthDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIFont *>*fontDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *, NSNumber *> *cornerRadiusDictionaryM;
  1. 添加方法,用来传入相应的属性
  1. 方法需要返回一个block,这个block的返回值需要是self
  2. block当做返回值,相当于顺传参数

.h

#import <UIKit/UIKit.h>


@interface UIButton (Handler)

/**
 * @brief 设置state对应的button样式
 *
 * 当调用adjustButtonStyleWithState方法时,button才会改变到相应的样式
 * @warning 注意: 非线程安全,到主线程更新参数
 */
- (void)setUpStyle:(UIControlState) state style:(void(^)(UIButton *button))callBack;

/**
 * @brief 改变通过 setUpStyle:do: 方法设置好的button样式
 *
 * @warning 注意: 如果某个样式没有找到相应state下的值,就会设置成normal状态下的样式 如果normal状态下仍未找到,则保持当前状态
 */
- (void) adjustButtonStyleWithState: (UIControlState) state;


/**
 * @brief 设置normal 状态下的Font
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置state对应的font
 */
- (UIButton *(^)(UIFont *)) setUpFont;

/**
 * @brief 设置normal 状态下的titleColor
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置state对应的titleColor
 */
- (UIButton *(^)(UIColor *)) setUpTitleColor;

/**
 * @brief 设置normal 状态下的背景色
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置state对应的backgroundColor
 */
- (UIButton *(^)(UIColor *)) setUpBackgroundColor;

/**
 * @brief 设置normal 状态下的Font
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置state对应的title
 */
- (UIButton *(^)(NSString *)) setUpTitle;

/**
 * @brief 设置normal 状态下的】AttributedString
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置state对应的AttributedString
 */
- (UIButton *(^)(NSAttributedString *)) setUpAttributedString;


/**
 * @brief 设置normal 状态下的image
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置的是对应state的image
 */
- (UIButton *(^)(UIImage *)) setUpImage;

/**
 * @brief 设置normal 状态下的backgroundImage
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置的是对应state的backgroundImage
 */

- (UIButton *(^)(UIImage *)) setUpBackgroundImage;

/**
 * @brief 设置normal 状态下的layer BorderWidth
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置的是对应state的BorderWidth
 */
- (UIButton *(^)(CGFloat)) setUpBorderWidth;

/**
 * @brief 设置normal 状态下的layer border color
 *
 * @return 返回 一个返回self的block.
 *
 * @warning 注意: 在 setUpStyle:setUp: 方法中 设置的是对应state的BorderColor
 */
- (UIButton *(^)(UIColor *)) setUpBorderColor;
@end

.m

#import "UIButton+Handler.h"
#import <objc/runtime.h>

static NSString *const k_setupState = @"k_setupState";
static NSString *const k_setupBackgroundColorDictionaryM = @"k_setupBackgroundColorDictionaryM";
static NSString *const k_borderColorDictionaryM = @"k_borderColorDictionaryM";
static NSString *const k_borderWidthDictionaryM = @"k_borderWidthDictionaryM";
static NSString *const k_fontDictionaryM = @"k_fontDictionaryM";
static NSString *const k_cornerRadiusDictionaryM = @"k_cornerRadiusDictionaryM";
@interface UIButton (Handler)
@property (nonatomic,assign) UIControlState setupState;

@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIColor *>*backgroundColorDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIColor *>*borderColorDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,NSNumber *>*borderWidthDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *,UIFont *>*fontDictionaryM;
@property (nonatomic,strong) NSMutableDictionary <NSNumber *, NSNumber *> *cornerRadiusDictionaryM;
@end

@implementation UIButton (Handler)

- (UIButton *(^)(UIFont *)) setUpFont {
    __weak typeof (self)weakSelf = self;
    return ^(UIFont *font) {
        if (weakSelf.state == weakSelf.setupState) {
            weakSelf.titleLabel.font = font;
        }
        weakSelf.fontDictionaryM[@(weakSelf.setupState)] = font;
        return weakSelf;
    };
}

- (UIButton *(^)(CGFloat)) setUpCornerRadius {
    __weak typeof (self) weakSelf = self;
    return ^(CGFloat radius) {
        if (weakSelf.setupState == weakSelf.state) {
            weakSelf.layer.cornerRadius = radius;
        }
        weakSelf.cornerRadiusDictionaryM[@(weakSelf.setupState)] = @(radius);
        return weakSelf;
    };
}

- (void)setUpStyle:(UIControlState) state style:(void(^)(UIButton *button))callBack {
        self.setupState = state;
        callBack(self);
        self.setupState = UIControlStateNormal;
}

- (UIButton *(^)(UIColor *)) setUpTitleColor {
    __weak typeof (self)weakSelf = self;
    return ^(UIColor *color) {
        [weakSelf setTitleColor:color forState:weakSelf.setupState];
        return weakSelf;
    };
}

- (UIButton *(^)(UIColor *)) setUpBackgroundColor {
    __weak typeof (self)weakSelf = self;
    return ^(UIColor *color) {
        weakSelf.backgroundColor = color;
        weakSelf.backgroundColorDictionaryM[@(weakSelf.setupState)] = color;
        return weakSelf;
    };
}

- (UIButton *(^)(NSString *)) setUpTitle {
    __weak typeof (self)weakSelf = self;
    return ^(NSString *title) {
        [weakSelf setTitle:title forState:weakSelf.setupState];
        return weakSelf;
    };
}

- (UIButton *(^)(NSAttributedString *)) setUpAttributedString {
    __weak typeof (self)weakSelf = self;
    return ^(NSAttributedString *attributedString) {
        [weakSelf setAttributedTitle:attributedString forState:weakSelf.setupState];
        return weakSelf;
    };
}

- (UIButton *(^)(UIImage *))setUpImage {
    __weak typeof (self)weakSelf = self;
    return ^(UIImage *image) {
        [weakSelf setImage:image forState:weakSelf.setupState];
        return weakSelf;
    };
}

- (UIButton *(^)(UIImage *))setUpBackgroundImage {
    __weak typeof (self)weakSelf = self;
    return ^(UIImage *image) {
        [weakSelf setBackgroundImage:image forState:weakSelf.setupState];
        return weakSelf;
    };
}

- (UIButton *(^)(CGFloat)) setUpBorderWidth {
    __weak typeof (self)weakSelf = self;
    return ^(CGFloat borderW) {
        if (weakSelf.state == weakSelf.setupState) {
            weakSelf.layer.borderWidth = borderW;
        }
        weakSelf.borderWidthDictionaryM[@(weakSelf.setupState)] = @(borderW);
        return weakSelf;
    };
}

- (UIButton *(^)(UIColor *)) setUpBorderColor {
    __weak typeof (self)weakSelf = self;
    return ^(UIColor *color) {
        if (weakSelf.state == weakSelf.setupState) {
            weakSelf.layer.borderColor = color.CGColor;
        }
        weakSelf.borderColorDictionaryM[@(weakSelf.setupState)] = color;
        return weakSelf;
    };
}

- (void) adjustButtonStyleWithState: (UIControlState) state {
    [self adjustBackgroundColorWithState:state];
    [self adjustBorderColorWithState:state];
    [self adjustBorderWidthWithState:state];
    [self adjustFontWithState:state];
    [self adjustCornerRadiusWithState:state];
}

- (void) adjustBackgroundColorWithState:(UIControlState)state {
    NSNumber *currenState = @(state);
    NSNumber *normalState = @(UIControlStateNormal);
    UIColor *backgroundColor = self.backgroundColorDictionaryM[currenState];
    
    /// backgroundColor
    if (!backgroundColor) {
        backgroundColor = self.backgroundColorDictionaryM[normalState];
    }
    if (backgroundColor) {
        self.backgroundColor = backgroundColor;
    }
    
}
- (void) adjustBorderColorWithState:(UIControlState)state {
    NSNumber *currenState = @(state);
    NSNumber *normalState = @(UIControlStateNormal);
    // borderColor
    UIColor *borderColor = self.borderColorDictionaryM[currenState];
    if (!borderColor) {
        borderColor = self.borderColorDictionaryM[normalState];
    }
    if (borderColor) {
        self.layer.borderColor = borderColor.CGColor;
    }
}
- (void) adjustBorderWidthWithState:(UIControlState)state {
    NSNumber *currenState = @(state);
    NSNumber *normalState = @(UIControlStateNormal);
    //borderWidth
    NSNumber *borderWidthNumbser = self.borderWidthDictionaryM[currenState];
    if (!borderWidthNumbser) {
        borderWidthNumbser = self.borderWidthDictionaryM[normalState];
    }
    CGFloat borderWidth = borderWidthNumbser.floatValue;
    self.layer.borderWidth = borderWidth;
}
- (void) adjustFontWithState:(UIControlState)state {
    NSNumber *currenState = @(state);
    NSNumber *normalState = @(UIControlStateNormal);
    //borderWidth
    UIFont *font = self.fontDictionaryM[currenState];
    if (!font) {
        font = self.fontDictionaryM[normalState];
    }
    if (font) {
        self.titleLabel.font = font;
    }
}
- (void) adjustCornerRadiusWithState:(UIControlState)state {
    NSNumber *currenState = @(state);
    NSNumber *normalState = @(UIControlStateNormal);
    //borderWidth
    NSNumber *value = self.cornerRadiusDictionaryM[currenState];
    if (!value) {
        value = self.cornerRadiusDictionaryM[normalState];
    }
    self.layer.cornerRadius = value.floatValue;
}

//MAKR: - get && set
- (void)setSetupState:(UIControlState)setupState {
    objc_setAssociatedObject(self, &k_setupState, @(setupState), OBJC_ASSOCIATION_ASSIGN);
}

- (UIControlState) setupState {
    NSNumber *state = objc_getAssociatedObject(self, &k_setupState);
    return state.integerValue;
}


- (void) setBackgroundColorDictionaryM:(NSMutableDictionary<NSNumber *,UIColor *> *)backgroundColorDictionaryM {
    objc_setAssociatedObject(self, &k_setupBackgroundColorDictionaryM, backgroundColorDictionaryM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary<NSNumber *,UIColor *> *)backgroundColorDictionaryM {
    NSMutableDictionary<NSNumber *,UIColor *> *dic = objc_getAssociatedObject(self, &k_setupBackgroundColorDictionaryM);
    if (!dic) {
        dic = [NSMutableDictionary new];
        objc_setAssociatedObject(self, &k_setupBackgroundColorDictionaryM, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return dic;
}
- (void) setBorderColorDictionaryM:(NSMutableDictionary<NSNumber *,UIColor *> *)borderColorDictionaryM {
     objc_setAssociatedObject(self, &k_borderColorDictionaryM, borderColorDictionaryM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary<NSNumber *,UIColor *> *)borderColorDictionaryM {
    NSMutableDictionary<NSNumber *,UIColor *> *dic = objc_getAssociatedObject(self, &k_borderColorDictionaryM);
    if (!dic) {
        dic = [NSMutableDictionary new];
        objc_setAssociatedObject(self, &k_borderColorDictionaryM, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return dic;
}

- (void)setBorderWidthDictionaryM:(NSMutableDictionary<NSNumber *,NSNumber *> *)borderWidthDictionaryM {
     objc_setAssociatedObject(self, &k_borderWidthDictionaryM, borderWidthDictionaryM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (NSMutableDictionary<NSNumber *,NSNumber *> *)borderWidthDictionaryM {
    NSMutableDictionary<NSNumber *,NSNumber *> *dic = objc_getAssociatedObject(self, &k_borderWidthDictionaryM);
    if (!dic) {
        dic = [NSMutableDictionary new];
         objc_setAssociatedObject(self, &k_borderWidthDictionaryM, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return dic;
}

- (void)setFontDictionaryM:(NSMutableDictionary<NSNumber *,UIFont *> *)fontDictionaryM {
    objc_setAssociatedObject(self, &k_fontDictionaryM, fontDictionaryM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary<NSNumber *,UIFont *> *)fontDictionaryM {
    NSMutableDictionary<NSNumber *,UIFont *> *dic = objc_getAssociatedObject(self, &k_fontDictionaryM);
    if (!dic) {
        dic = [NSMutableDictionary new];
        objc_setAssociatedObject(self, &k_fontDictionaryM, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return dic;
}

- (void)setCornerRadiusDictionaryM:(NSMutableDictionary<NSNumber *,NSNumber *> *)cornerRadiusDictionaryM {
    objc_setAssociatedObject(self, &k_cornerRadiusDictionaryM, cornerRadiusDictionaryM, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSMutableDictionary<NSNumber *,NSNumber *> *)cornerRadiusDictionaryM {
    NSMutableDictionary<NSNumber *,NSNumber *> *dic = objc_getAssociatedObject(self, &k_cornerRadiusDictionaryM);
    if (!dic) {
        dic = [NSMutableDictionary new];
        objc_setAssociatedObject(self, &k_cornerRadiusDictionaryM, dic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return dic;
}
@end

下载 demo点这里 希望对你有点帮助👌👌

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,306评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,657评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,928评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,688评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,105评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,024评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,159评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,937评论 0 212
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,689评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,851评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,325评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,651评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,364评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,192评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,985评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,154评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,955评论 2 279

推荐阅读更多精彩内容