测试测试 | ✗ |
语言语言 | Obj-CObjective C |
许可 | MIT |
发布最后发布 | 2017年7月 |
由 spWang--description=macbook pro 维护。
为按钮的 backgroundColor、borderWidth、borderColor 和 titleLabelFont 配置不同的状态属性
pod 'UIButtonState','1.0.2'
#import "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]}];
* ARC
* iOS>=6.0
一个轻松配置您的按钮在不同背景颜色、边框宽度和字体大小的方法
* 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"`
@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
//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]}];
* ARC
* iOS>=6.0