UIButton-State
UIButton-State copied to clipboard
一句代码为UIbutton增加不同状态的配置(borderColor, borderWidth, titleLabelFont, backgroundColor,subView)
UIButton-State
为按钮的backgroundColor、borderWidth borderColor and titleLabelFont配置不同状态下的属性

如何使用
- 安装CocoaPods:
pod 'UIButtonState','1.0.2'
- 文件拖拽:
- 将UIButton+State文件夹的文件拖到你的项目中
- 导入头文件:
#import "UIButton+HCBState.h"
UIButton+HCBState.h
/** 获取当前borderColor */
@property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBorderColor;
/** 获取当前backgroundColor */
@property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBackgroundColor;
/** 获取当前borderWidth */
@property (nonatomic, readonly, assign) CGFloat hcb_currentBorderWidth;
/** 获取当前titleLabelFont */
@property(nonatomic, readonly, strong) UIFont *hcb_currentTitleLabelFont;
/** 设置不同状态下的borderColor(支持动画效果),需先设置borderWidth */
- (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated;
/** 设置不同状态下的borderWidth(支持动画效果) */
- (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated;
/** 设置不同状态下的backgroundColor(支持动画效果) */
- (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated;
/** 设置不同状态下的titleLabelFont */
- (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state;
/** 获取某个状态的borderColor */
- (nullable UIColor *)hcb_borderColorForState:(UIControlState)state;
/** 获取某个状态的borderWidth */
- (CGFloat)hcb_borderWidthForState:(UIControlState)state;
/** 获取某个状态的backgroundColor */
- (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state;
/** 获取某个状态的titleLabelFont */
- (UIFont *)hcb_titleLabelFontForState:(UIControlState)state;
/** 为自己的subView设置不同状态下的属性 */
- (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag;
#pragma mark - 使用key-value方式设置
/** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画),需先设置borderWidth */
- (void)hcb_configBorderColors:(NSDictionary <NSNumber *,UIColor *>*)borderColors;
/** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画) */
- (void)hcb_configBackgroundColors:(NSDictionary <NSNumber *,UIColor *>*)backgroundColors;
/** key:需要将UIControlState枚举包装为NSNumber类型即可 */
- (void)hcb_configTitleLabelFont:(NSDictionary <NSNumber *,UIFont *>*)titleLabelFonts;
/** 切换按钮状态时,执行动画的时间,默认0.25s(只有动画执行完毕后,才能会执行下一个点击事件) */
@property (nonatomic, assign) NSTimeInterval hcb_animatedDuration;
@end
例子
//setBackgroundColor
1. [button hcb_setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal animated:YES];
//setborderColor
2. button.layer.borderWidth = 10;
[button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:YES];
//setborderWidth
3. button.layer.borderColor = [UIColor redColor].CGColor;
[button hcb_setborderWidth:3 forState:UIControlStateNormal animated:YES];
[button hcb_setborderWidth:20 forState:UIControlStateSelected animated:YES];
//setTitleLabelFont
4. [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:10] forState:UIControlStateNormal];
//配置SubView
5. [button hcb_setSubViewValue:@(NSTextAlignmentLeft) forKeyPath:@"textAlignment" forState:UIControlStateNormal withSubViewTag:10001];
//使用key-Value方案来配置
6. [button hcb_configBackgroundColors:@{@(UIControlStateNormal) : [UIColor redColor], @(UIControlStateSelected) : [UIColor blueColor]}];
Remind
* ARC
* iOS>=6.0
for English
UIButton-State
a easy way to config your button with different backgroundColor、borderWidth borderColor and titleLabelFont
How to use it
* Installation with CocoaPods:`pod 'UIButtonState','1.0.2'`
* Manual import:
* Drag All files in the `UIButton+State` folder to project
* Import the file:`#import "UIButton+HCBState.h"`
UIButton+HCBState.h
@interface UIButton (HCBState)
/** get the current borderColor */
@property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBorderColor;
/** get the current backgroundColor */
@property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBackgroundColor;
/** get the current titleLabelFont */
@property(nonatomic, readonly, strong) UIFont *hcb_currentTitleLabelFont;
/** setting borderColor for different state(support animation) */
- (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated;
/** setting borderWidth for different state(support animation) */
- (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated;
/** setting backgroundColor for different state(support animation) */
- (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated;
/** setting titleLabelFont for different state */
- (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state;
/** get the borderColor for state */
- (nullable UIColor *)hcb_borderColorForState:(UIControlState)state;
/** get the borderWidth for state */
- (CGFloat)hcb_borderWidthForState:(UIControlState)state;
/** get the backgroundColor for state */
- (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state;
/** get the titleLabelFont for state */
- (UIFont *)hcb_titleLabelFontForState:(UIControlState)state;
/** config your subView */
- (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag;
#pragma mark - use key-value
/** key:UIControlState should be NSNumber class(this way is not support animation) */
- (void)hcb_configBorderColors:(NSDictionary <NSNumber *,UIColor *>*)borderColors;
/** key:UIControlState should be NSNumber class(this way is not support animation) */
- (void)hcb_configBackgroundColors:(NSDictionary <NSNumber *,UIColor *>*)backgroundColors;
/** key:UIControlState should be NSNumber class(this way is not support animation) */
- (void)hcb_configTitleLabelFont:(NSDictionary <NSNumber *,UIFont *>*)titleLabelFonts;
/** when button state change, this property is the duration for animation, default is 0.25s(when last animation is ended, the next animation will execute) */
@property (nonatomic, assign) NSTimeInterval hcb_animatedDuration;
@end
for example
//setBackgroundColor
1. [button hcb_setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal animated:YES];
//setborderColor
2. button.layer.borderWidth = 10;
[button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:YES];
//setborderWidth
3. button.layer.borderColor = [UIColor redColor].CGColor;
[button hcb_setborderWidth:3 forState:UIControlStateNormal animated:YES];
[button hcb_setborderWidth:20 forState:UIControlStateSelected animated:YES];
//setTitleLabelFont
4. [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:10] forState:UIControlStateNormal];
//set SubView use key-Value
5. [button hcb_setSubViewValue:@(NSTextAlignmentLeft) forKeyPath:@"textAlignment" forState:UIControlStateNormal withSubViewTag:10001];
//use key-Value config BackgroundColors
6. [button hcb_configBackgroundColors:@{@(UIControlStateNormal) : [UIColor redColor], @(UIControlStateSelected) : [UIColor blueColor]}];
Remind
* ARC
* iOS>=6.0
Hope
- If you find bug when used,Hope you can Issues me,Thank you or try to download the latest code of this extension to see the BUG has been fixed or not