├── .gitignore ├── LICENSE ├── README.md ├── UIButtonState.podspec ├── UIButtonState ├── UIButtonState.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── UIButtonState │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── ColorConfig.h │ ├── Info.plist │ ├── UIButton+State │ ├── UIButton+HCBState.h │ └── UIButton+HCBState.m │ ├── UIColor+Hex.h │ ├── UIColor+Hex.m │ ├── ViewController.h │ ├── ViewController.m │ └── main.m └── demo.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | 55 | #Code Injection 56 | # 57 | # After new code Injection tools there's a generated folder /iOSInjectionProject 58 | # https://github.com/johnno1962/injectionforxcode 59 | 60 | iOSInjectionProject/ 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 spWang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UIButton-State 2 | 为按钮的backgroundColor、borderWidth borderColor and titleLabelFont配置不同状态下的属性 3 | 4 | 5 | 6 | ## 如何使用 7 | * 安装CocoaPods:`pod 'UIButtonState','1.0.2'` 8 | * 文件拖拽: 9 | * 将UIButton+State文件夹的文件拖到你的项目中 10 | * 导入头文件:`#import "UIButton+HCBState.h"` 11 | 12 | ## UIButton+HCBState.h 13 | ```objc 14 | /** 获取当前borderColor */ 15 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBorderColor; 16 | 17 | /** 获取当前backgroundColor */ 18 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBackgroundColor; 19 | 20 | /** 获取当前borderWidth */ 21 | @property (nonatomic, readonly, assign) CGFloat hcb_currentBorderWidth; 22 | 23 | /** 获取当前titleLabelFont */ 24 | @property(nonatomic, readonly, strong) UIFont *hcb_currentTitleLabelFont; 25 | 26 | 27 | /** 设置不同状态下的borderColor(支持动画效果),需先设置borderWidth */ 28 | - (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated; 29 | 30 | /** 设置不同状态下的borderWidth(支持动画效果) */ 31 | - (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated; 32 | 33 | /** 设置不同状态下的backgroundColor(支持动画效果) */ 34 | - (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated; 35 | 36 | /** 设置不同状态下的titleLabelFont */ 37 | - (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state; 38 | 39 | /** 获取某个状态的borderColor */ 40 | - (nullable UIColor *)hcb_borderColorForState:(UIControlState)state; 41 | 42 | /** 获取某个状态的borderWidth */ 43 | - (CGFloat)hcb_borderWidthForState:(UIControlState)state; 44 | 45 | /** 获取某个状态的backgroundColor */ 46 | - (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state; 47 | 48 | /** 获取某个状态的titleLabelFont */ 49 | - (UIFont *)hcb_titleLabelFontForState:(UIControlState)state; 50 | 51 | 52 | /** 为自己的subView设置不同状态下的属性 */ 53 | - (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag; 54 | 55 | 56 | #pragma mark - 使用key-value方式设置 57 | /** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画),需先设置borderWidth */ 58 | - (void)hcb_configBorderColors:(NSDictionary *)borderColors; 59 | 60 | /** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画) */ 61 | - (void)hcb_configBackgroundColors:(NSDictionary *)backgroundColors; 62 | 63 | /** key:需要将UIControlState枚举包装为NSNumber类型即可 */ 64 | - (void)hcb_configTitleLabelFont:(NSDictionary *)titleLabelFonts; 65 | 66 | 67 | /** 切换按钮状态时,执行动画的时间,默认0.25s(只有动画执行完毕后,才能会执行下一个点击事件) */ 68 | @property (nonatomic, assign) NSTimeInterval hcb_animatedDuration; 69 | 70 | 71 | @end 72 | ``` 73 | ## 例子 74 | ```objc 75 | //setBackgroundColor 76 | 1. [button hcb_setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal animated:YES]; 77 | 78 | //setborderColor 79 | 2. button.layer.borderWidth = 10; 80 | [button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:YES]; 81 | 82 | //setborderWidth 83 | 3. button.layer.borderColor = [UIColor redColor].CGColor; 84 | [button hcb_setborderWidth:3 forState:UIControlStateNormal animated:YES]; 85 | [button hcb_setborderWidth:20 forState:UIControlStateSelected animated:YES]; 86 | 87 | //setTitleLabelFont 88 | 4. [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:10] forState:UIControlStateNormal]; 89 | 90 | //配置SubView 91 | 5. [button hcb_setSubViewValue:@(NSTextAlignmentLeft) forKeyPath:@"textAlignment" forState:UIControlStateNormal withSubViewTag:10001]; 92 | 93 | //使用key-Value方案来配置 94 | 6. [button hcb_configBackgroundColors:@{@(UIControlStateNormal) : [UIColor redColor], @(UIControlStateSelected) : [UIColor blueColor]}]; 95 | ``` 96 | 97 | ## Remind 98 | ```objc 99 | * ARC 100 | * iOS>=6.0 101 | ``` 102 | 103 | 104 | # for English 105 | 106 | # UIButton-State 107 | a easy way to config your button with different backgroundColor、borderWidth borderColor and titleLabelFont 108 | 109 | ## How to use it 110 | ```objc 111 | * Installation with CocoaPods:`pod 'UIButtonState','1.0.2'` 112 | * Manual import: 113 | * Drag All files in the `UIButton+State` folder to project 114 | * Import the file:`#import "UIButton+HCBState.h"` 115 | ``` 116 | 117 | ## UIButton+HCBState.h 118 | ```objc 119 | @interface UIButton (HCBState) 120 | /** get the current borderColor */ 121 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBorderColor; 122 | 123 | /** get the current backgroundColor */ 124 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBackgroundColor; 125 | 126 | /** get the current titleLabelFont */ 127 | @property(nonatomic, readonly, strong) UIFont *hcb_currentTitleLabelFont; 128 | 129 | /** setting borderColor for different state(support animation) */ 130 | - (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated; 131 | 132 | /** setting borderWidth for different state(support animation) */ 133 | - (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated; 134 | 135 | /** setting backgroundColor for different state(support animation) */ 136 | - (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated; 137 | 138 | /** setting titleLabelFont for different state */ 139 | - (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state; 140 | 141 | /** get the borderColor for state */ 142 | - (nullable UIColor *)hcb_borderColorForState:(UIControlState)state; 143 | 144 | /** get the borderWidth for state */ 145 | - (CGFloat)hcb_borderWidthForState:(UIControlState)state; 146 | 147 | /** get the backgroundColor for state */ 148 | - (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state; 149 | 150 | /** get the titleLabelFont for state */ 151 | - (UIFont *)hcb_titleLabelFontForState:(UIControlState)state; 152 | 153 | 154 | /** config your subView */ 155 | - (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag; 156 | 157 | 158 | #pragma mark - use key-value 159 | /** key:UIControlState should be NSNumber class(this way is not support animation) */ 160 | - (void)hcb_configBorderColors:(NSDictionary *)borderColors; 161 | 162 | /** key:UIControlState should be NSNumber class(this way is not support animation) */ 163 | - (void)hcb_configBackgroundColors:(NSDictionary *)backgroundColors; 164 | 165 | /** key:UIControlState should be NSNumber class(this way is not support animation) */ 166 | - (void)hcb_configTitleLabelFont:(NSDictionary *)titleLabelFonts; 167 | 168 | /** 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) */ 169 | @property (nonatomic, assign) NSTimeInterval hcb_animatedDuration; 170 | 171 | @end 172 | ``` 173 | 174 | ## for example 175 | ```objc 176 | //setBackgroundColor 177 | 1. [button hcb_setBackgroundColor:[UIColor redColor] forState:UIControlStateNormal animated:YES]; 178 | 179 | //setborderColor 180 | 2. button.layer.borderWidth = 10; 181 | [button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:YES]; 182 | 183 | //setborderWidth 184 | 3. button.layer.borderColor = [UIColor redColor].CGColor; 185 | [button hcb_setborderWidth:3 forState:UIControlStateNormal animated:YES]; 186 | [button hcb_setborderWidth:20 forState:UIControlStateSelected animated:YES]; 187 | 188 | //setTitleLabelFont 189 | 4. [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:10] forState:UIControlStateNormal]; 190 | 191 | //set SubView use key-Value 192 | 5. [button hcb_setSubViewValue:@(NSTextAlignmentLeft) forKeyPath:@"textAlignment" forState:UIControlStateNormal withSubViewTag:10001]; 193 | 194 | //use key-Value config BackgroundColors 195 | 6. [button hcb_configBackgroundColors:@{@(UIControlStateNormal) : [UIColor redColor], @(UIControlStateSelected) : [UIColor blueColor]}]; 196 | ``` 197 | 198 | ## Remind 199 | ```objc 200 | * ARC 201 | * iOS>=6.0 202 | ``` 203 | 204 | ## Hope 205 | * 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 206 | -------------------------------------------------------------------------------- /UIButtonState.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'UIButtonState' 3 | s.version = '1.0.2' 4 | s.summary = 'An easy way to use config button state' 5 | s.homepage = 'https://github.com/spWang/UIButton-State' 6 | s.license = 'MIT' 7 | s.authors = {'spWang' => 'wsp810@163.com'} 8 | s.platform = :ios, '7.0' 9 | s.source = {:git => 'https://github.com/spWang/UIButton-State.git', :tag => s.version} 10 | s.source_files = 'UIButtonState/UIButtonState/UIButton+State/*.{h,m}' 11 | s.requires_arc = true 12 | s.frameworks = 'UIKit' 13 | end 14 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | DA4875DA1DB0B87700422A20 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4875D91DB0B87700422A20 /* main.m */; }; 11 | DA4875DD1DB0B87700422A20 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4875DC1DB0B87700422A20 /* AppDelegate.m */; }; 12 | DA4875E01DB0B87700422A20 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4875DF1DB0B87700422A20 /* ViewController.m */; }; 13 | DA4875E31DB0B87700422A20 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DA4875E11DB0B87700422A20 /* Main.storyboard */; }; 14 | DA4875E51DB0B87700422A20 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DA4875E41DB0B87700422A20 /* Assets.xcassets */; }; 15 | DA4875E81DB0B87700422A20 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DA4875E61DB0B87700422A20 /* LaunchScreen.storyboard */; }; 16 | DA4875F21DB0BAF600422A20 /* UIButton+HCBState.m in Sources */ = {isa = PBXBuildFile; fileRef = DA4875F11DB0BAF600422A20 /* UIButton+HCBState.m */; }; 17 | DAF3D2EA1F27568E0056568E /* UIColor+Hex.m in Sources */ = {isa = PBXBuildFile; fileRef = DAF3D2E91F27568E0056568E /* UIColor+Hex.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | DA4875D51DB0B87700422A20 /* UIButtonState.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UIButtonState.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | DA4875D91DB0B87700422A20 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | DA4875DB1DB0B87700422A20 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | DA4875DC1DB0B87700422A20 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | DA4875DE1DB0B87700422A20 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 26 | DA4875DF1DB0B87700422A20 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 27 | DA4875E21DB0B87700422A20 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | DA4875E41DB0B87700422A20 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | DA4875E71DB0B87700422A20 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | DA4875E91DB0B87700422A20 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | DA4875F01DB0BAF600422A20 /* UIButton+HCBState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIButton+HCBState.h"; sourceTree = ""; }; 32 | DA4875F11DB0BAF600422A20 /* UIButton+HCBState.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIButton+HCBState.m"; sourceTree = ""; }; 33 | DAF3D2E81F27568E0056568E /* UIColor+Hex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+Hex.h"; sourceTree = ""; }; 34 | DAF3D2E91F27568E0056568E /* UIColor+Hex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIColor+Hex.m"; sourceTree = ""; }; 35 | DAF3D2EB1F2756C80056568E /* ColorConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ColorConfig.h; sourceTree = ""; }; 36 | /* End PBXFileReference section */ 37 | 38 | /* Begin PBXFrameworksBuildPhase section */ 39 | DA4875D21DB0B87700422A20 /* Frameworks */ = { 40 | isa = PBXFrameworksBuildPhase; 41 | buildActionMask = 2147483647; 42 | files = ( 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | DA4875CC1DB0B87700422A20 = { 50 | isa = PBXGroup; 51 | children = ( 52 | DA4875D71DB0B87700422A20 /* UIButtonState */, 53 | DA4875D61DB0B87700422A20 /* Products */, 54 | ); 55 | sourceTree = ""; 56 | }; 57 | DA4875D61DB0B87700422A20 /* Products */ = { 58 | isa = PBXGroup; 59 | children = ( 60 | DA4875D51DB0B87700422A20 /* UIButtonState.app */, 61 | ); 62 | name = Products; 63 | sourceTree = ""; 64 | }; 65 | DA4875D71DB0B87700422A20 /* UIButtonState */ = { 66 | isa = PBXGroup; 67 | children = ( 68 | DA4875EF1DB0B8C300422A20 /* UIButton+State */, 69 | DA4875DB1DB0B87700422A20 /* AppDelegate.h */, 70 | DA4875DC1DB0B87700422A20 /* AppDelegate.m */, 71 | DA4875DE1DB0B87700422A20 /* ViewController.h */, 72 | DA4875DF1DB0B87700422A20 /* ViewController.m */, 73 | DAF3D2EB1F2756C80056568E /* ColorConfig.h */, 74 | DAF3D2E81F27568E0056568E /* UIColor+Hex.h */, 75 | DAF3D2E91F27568E0056568E /* UIColor+Hex.m */, 76 | DA4875E11DB0B87700422A20 /* Main.storyboard */, 77 | DA4875E41DB0B87700422A20 /* Assets.xcassets */, 78 | DA4875E61DB0B87700422A20 /* LaunchScreen.storyboard */, 79 | DA4875E91DB0B87700422A20 /* Info.plist */, 80 | DA4875D81DB0B87700422A20 /* Supporting Files */, 81 | ); 82 | path = UIButtonState; 83 | sourceTree = ""; 84 | }; 85 | DA4875D81DB0B87700422A20 /* Supporting Files */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | DA4875D91DB0B87700422A20 /* main.m */, 89 | ); 90 | name = "Supporting Files"; 91 | sourceTree = ""; 92 | }; 93 | DA4875EF1DB0B8C300422A20 /* UIButton+State */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | DA4875F01DB0BAF600422A20 /* UIButton+HCBState.h */, 97 | DA4875F11DB0BAF600422A20 /* UIButton+HCBState.m */, 98 | ); 99 | path = "UIButton+State"; 100 | sourceTree = ""; 101 | }; 102 | /* End PBXGroup section */ 103 | 104 | /* Begin PBXNativeTarget section */ 105 | DA4875D41DB0B87700422A20 /* UIButtonState */ = { 106 | isa = PBXNativeTarget; 107 | buildConfigurationList = DA4875EC1DB0B87700422A20 /* Build configuration list for PBXNativeTarget "UIButtonState" */; 108 | buildPhases = ( 109 | DA4875D11DB0B87700422A20 /* Sources */, 110 | DA4875D21DB0B87700422A20 /* Frameworks */, 111 | DA4875D31DB0B87700422A20 /* Resources */, 112 | ); 113 | buildRules = ( 114 | ); 115 | dependencies = ( 116 | ); 117 | name = UIButtonState; 118 | productName = UIButtonState; 119 | productReference = DA4875D51DB0B87700422A20 /* UIButtonState.app */; 120 | productType = "com.apple.product-type.application"; 121 | }; 122 | /* End PBXNativeTarget section */ 123 | 124 | /* Begin PBXProject section */ 125 | DA4875CD1DB0B87700422A20 /* Project object */ = { 126 | isa = PBXProject; 127 | attributes = { 128 | LastUpgradeCheck = 0800; 129 | ORGANIZATIONNAME = "Mac-pro"; 130 | TargetAttributes = { 131 | DA4875D41DB0B87700422A20 = { 132 | CreatedOnToolsVersion = 8.0; 133 | DevelopmentTeam = W32W3P434E; 134 | ProvisioningStyle = Automatic; 135 | }; 136 | }; 137 | }; 138 | buildConfigurationList = DA4875D01DB0B87700422A20 /* Build configuration list for PBXProject "UIButtonState" */; 139 | compatibilityVersion = "Xcode 3.2"; 140 | developmentRegion = English; 141 | hasScannedForEncodings = 0; 142 | knownRegions = ( 143 | en, 144 | Base, 145 | ); 146 | mainGroup = DA4875CC1DB0B87700422A20; 147 | productRefGroup = DA4875D61DB0B87700422A20 /* Products */; 148 | projectDirPath = ""; 149 | projectRoot = ""; 150 | targets = ( 151 | DA4875D41DB0B87700422A20 /* UIButtonState */, 152 | ); 153 | }; 154 | /* End PBXProject section */ 155 | 156 | /* Begin PBXResourcesBuildPhase section */ 157 | DA4875D31DB0B87700422A20 /* Resources */ = { 158 | isa = PBXResourcesBuildPhase; 159 | buildActionMask = 2147483647; 160 | files = ( 161 | DA4875E81DB0B87700422A20 /* LaunchScreen.storyboard in Resources */, 162 | DA4875E51DB0B87700422A20 /* Assets.xcassets in Resources */, 163 | DA4875E31DB0B87700422A20 /* Main.storyboard in Resources */, 164 | ); 165 | runOnlyForDeploymentPostprocessing = 0; 166 | }; 167 | /* End PBXResourcesBuildPhase section */ 168 | 169 | /* Begin PBXSourcesBuildPhase section */ 170 | DA4875D11DB0B87700422A20 /* Sources */ = { 171 | isa = PBXSourcesBuildPhase; 172 | buildActionMask = 2147483647; 173 | files = ( 174 | DA4875E01DB0B87700422A20 /* ViewController.m in Sources */, 175 | DAF3D2EA1F27568E0056568E /* UIColor+Hex.m in Sources */, 176 | DA4875F21DB0BAF600422A20 /* UIButton+HCBState.m in Sources */, 177 | DA4875DD1DB0B87700422A20 /* AppDelegate.m in Sources */, 178 | DA4875DA1DB0B87700422A20 /* main.m in Sources */, 179 | ); 180 | runOnlyForDeploymentPostprocessing = 0; 181 | }; 182 | /* End PBXSourcesBuildPhase section */ 183 | 184 | /* Begin PBXVariantGroup section */ 185 | DA4875E11DB0B87700422A20 /* Main.storyboard */ = { 186 | isa = PBXVariantGroup; 187 | children = ( 188 | DA4875E21DB0B87700422A20 /* Base */, 189 | ); 190 | name = Main.storyboard; 191 | sourceTree = ""; 192 | }; 193 | DA4875E61DB0B87700422A20 /* LaunchScreen.storyboard */ = { 194 | isa = PBXVariantGroup; 195 | children = ( 196 | DA4875E71DB0B87700422A20 /* Base */, 197 | ); 198 | name = LaunchScreen.storyboard; 199 | sourceTree = ""; 200 | }; 201 | /* End PBXVariantGroup section */ 202 | 203 | /* Begin XCBuildConfiguration section */ 204 | DA4875EA1DB0B87700422A20 /* Debug */ = { 205 | isa = XCBuildConfiguration; 206 | buildSettings = { 207 | ALWAYS_SEARCH_USER_PATHS = NO; 208 | CLANG_ANALYZER_NONNULL = YES; 209 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 210 | CLANG_CXX_LIBRARY = "libc++"; 211 | CLANG_ENABLE_MODULES = YES; 212 | CLANG_ENABLE_OBJC_ARC = YES; 213 | CLANG_WARN_BOOL_CONVERSION = YES; 214 | CLANG_WARN_CONSTANT_CONVERSION = YES; 215 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 216 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 217 | CLANG_WARN_EMPTY_BODY = YES; 218 | CLANG_WARN_ENUM_CONVERSION = YES; 219 | CLANG_WARN_INFINITE_RECURSION = YES; 220 | CLANG_WARN_INT_CONVERSION = YES; 221 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 222 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 223 | CLANG_WARN_UNREACHABLE_CODE = YES; 224 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 225 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 226 | COPY_PHASE_STRIP = NO; 227 | DEBUG_INFORMATION_FORMAT = dwarf; 228 | ENABLE_STRICT_OBJC_MSGSEND = YES; 229 | ENABLE_TESTABILITY = YES; 230 | GCC_C_LANGUAGE_STANDARD = gnu99; 231 | GCC_DYNAMIC_NO_PIC = NO; 232 | GCC_NO_COMMON_BLOCKS = YES; 233 | GCC_OPTIMIZATION_LEVEL = 0; 234 | GCC_PREPROCESSOR_DEFINITIONS = ( 235 | "DEBUG=1", 236 | "$(inherited)", 237 | ); 238 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 239 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 240 | GCC_WARN_UNDECLARED_SELECTOR = YES; 241 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 242 | GCC_WARN_UNUSED_FUNCTION = YES; 243 | GCC_WARN_UNUSED_VARIABLE = YES; 244 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 245 | MTL_ENABLE_DEBUG_INFO = YES; 246 | ONLY_ACTIVE_ARCH = YES; 247 | SDKROOT = iphoneos; 248 | TARGETED_DEVICE_FAMILY = "1,2"; 249 | }; 250 | name = Debug; 251 | }; 252 | DA4875EB1DB0B87700422A20 /* Release */ = { 253 | isa = XCBuildConfiguration; 254 | buildSettings = { 255 | ALWAYS_SEARCH_USER_PATHS = NO; 256 | CLANG_ANALYZER_NONNULL = YES; 257 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 258 | CLANG_CXX_LIBRARY = "libc++"; 259 | CLANG_ENABLE_MODULES = YES; 260 | CLANG_ENABLE_OBJC_ARC = YES; 261 | CLANG_WARN_BOOL_CONVERSION = YES; 262 | CLANG_WARN_CONSTANT_CONVERSION = YES; 263 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 264 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 265 | CLANG_WARN_EMPTY_BODY = YES; 266 | CLANG_WARN_ENUM_CONVERSION = YES; 267 | CLANG_WARN_INFINITE_RECURSION = YES; 268 | CLANG_WARN_INT_CONVERSION = YES; 269 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 270 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 271 | CLANG_WARN_UNREACHABLE_CODE = YES; 272 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 273 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 274 | COPY_PHASE_STRIP = NO; 275 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 276 | ENABLE_NS_ASSERTIONS = NO; 277 | ENABLE_STRICT_OBJC_MSGSEND = YES; 278 | GCC_C_LANGUAGE_STANDARD = gnu99; 279 | GCC_NO_COMMON_BLOCKS = YES; 280 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 281 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 282 | GCC_WARN_UNDECLARED_SELECTOR = YES; 283 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 284 | GCC_WARN_UNUSED_FUNCTION = YES; 285 | GCC_WARN_UNUSED_VARIABLE = YES; 286 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 287 | MTL_ENABLE_DEBUG_INFO = NO; 288 | SDKROOT = iphoneos; 289 | TARGETED_DEVICE_FAMILY = "1,2"; 290 | VALIDATE_PRODUCT = YES; 291 | }; 292 | name = Release; 293 | }; 294 | DA4875ED1DB0B87700422A20 /* Debug */ = { 295 | isa = XCBuildConfiguration; 296 | buildSettings = { 297 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 298 | DEVELOPMENT_TEAM = W32W3P434E; 299 | INFOPLIST_FILE = UIButtonState/Info.plist; 300 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 301 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 302 | PRODUCT_BUNDLE_IDENTIFIER = abcd.UIButtonState; 303 | PRODUCT_NAME = "$(TARGET_NAME)"; 304 | }; 305 | name = Debug; 306 | }; 307 | DA4875EE1DB0B87700422A20 /* Release */ = { 308 | isa = XCBuildConfiguration; 309 | buildSettings = { 310 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 311 | DEVELOPMENT_TEAM = W32W3P434E; 312 | INFOPLIST_FILE = UIButtonState/Info.plist; 313 | IPHONEOS_DEPLOYMENT_TARGET = 6.0; 314 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 315 | PRODUCT_BUNDLE_IDENTIFIER = abcd.UIButtonState; 316 | PRODUCT_NAME = "$(TARGET_NAME)"; 317 | }; 318 | name = Release; 319 | }; 320 | /* End XCBuildConfiguration section */ 321 | 322 | /* Begin XCConfigurationList section */ 323 | DA4875D01DB0B87700422A20 /* Build configuration list for PBXProject "UIButtonState" */ = { 324 | isa = XCConfigurationList; 325 | buildConfigurations = ( 326 | DA4875EA1DB0B87700422A20 /* Debug */, 327 | DA4875EB1DB0B87700422A20 /* Release */, 328 | ); 329 | defaultConfigurationIsVisible = 0; 330 | defaultConfigurationName = Release; 331 | }; 332 | DA4875EC1DB0B87700422A20 /* Build configuration list for PBXNativeTarget "UIButtonState" */ = { 333 | isa = XCConfigurationList; 334 | buildConfigurations = ( 335 | DA4875ED1DB0B87700422A20 /* Debug */, 336 | DA4875EE1DB0B87700422A20 /* Release */, 337 | ); 338 | defaultConfigurationIsVisible = 0; 339 | defaultConfigurationName = Release; 340 | }; 341 | /* End XCConfigurationList section */ 342 | }; 343 | rootObject = DA4875CD1DB0B87700422A20 /* Project object */; 344 | } 345 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 16/10/14. 6 | // Copyright © 2016年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 16/10/14. 6 | // Copyright © 2016年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/ColorConfig.h: -------------------------------------------------------------------------------- 1 | // 2 | // ColorConfig.h 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 17/7/25. 6 | // Copyright © 2017年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #ifndef ColorConfig_h 10 | #define ColorConfig_h 11 | #import "UIColor+Hex.h" 12 | 13 | #define color_orange [UIColor hcb_colorWithHex:0xFF7C3C] //统一橙色 14 | #define color_blue [UIColor hcb_colorWithHex:0x4298e5] //统一蓝色 15 | #define color_red [UIColor hcb_colorWithHex:0xff1e1e]//统一红色 16 | #define color_gray [UIColor hcb_colorWithHex:0x777777] //统一灰色 17 | #define color_lightGray [UIColor hcb_colorWithHex:0x999999] //统一浅灰色 18 | #define color_bg [UIColor hcb_colorWithHex:0xf3f6fa] //统一背景色 19 | #define color_yellow [UIColor hcb_colorWithHex:0xffce26]//统一黄色 20 | #define color_black [UIColor hcb_colorWithHex:0x1e2227] //统一黑色 21 | #define color_skyblue [UIColor hcb_colorWithHex:0x4FA0FB]//天蓝色 22 | 23 | #endif /* ColorConfig_h */ 24 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/UIButton+State/UIButton+HCBState.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+HCBState.h 3 | // ToolKit 4 | // 5 | // Created by wangshuaipeng on 16/9/9. 6 | // gitHub https://github.com/spWang/UIButton-State 7 | // Copyright © 2016年 Mac-pro. All rights reserved. 8 | // 1.0.2 9 | 10 | #import 11 | #import 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface UIButton (HCBState) 15 | /** 获取当前borderColor */ 16 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBorderColor; 17 | 18 | /** 获取当前backgroundColor */ 19 | @property(nullable, nonatomic, readonly, strong) UIColor *hcb_currentBackgroundColor; 20 | 21 | /** 获取当前borderWidth */ 22 | @property (nonatomic, readonly, assign) CGFloat hcb_currentBorderWidth; 23 | 24 | /** 获取当前titleLabelFont */ 25 | @property(nonatomic, readonly, strong) UIFont *hcb_currentTitleLabelFont; 26 | 27 | 28 | /** 设置不同状态下的borderColor(支持动画效果),需先设置borderWidth */ 29 | - (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated; 30 | 31 | /** 设置不同状态下的borderWidth(支持动画效果) */ 32 | - (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated; 33 | 34 | /** 设置不同状态下的backgroundColor(支持动画效果) */ 35 | - (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated; 36 | 37 | /** 设置不同状态下的titleLabelFont */ 38 | - (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state; 39 | 40 | /** 获取某个状态的borderColor */ 41 | - (nullable UIColor *)hcb_borderColorForState:(UIControlState)state; 42 | 43 | /** 获取某个状态的borderWidth */ 44 | - (CGFloat)hcb_borderWidthForState:(UIControlState)state; 45 | 46 | /** 获取某个状态的backgroundColor */ 47 | - (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state; 48 | 49 | /** 获取某个状态的titleLabelFont */ 50 | - (UIFont *)hcb_titleLabelFontForState:(UIControlState)state; 51 | 52 | 53 | /** 为自己的subView设置不同状态下的属性 */ 54 | - (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag; 55 | 56 | 57 | #pragma mark - 使用key-value方式设置 58 | /** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画),需先设置borderWidth */ 59 | - (void)hcb_configBorderColors:(NSDictionary *)borderColors; 60 | 61 | /** key:需要将UIControlState枚举包装为NSNumber类型即可(此方式无动画) */ 62 | - (void)hcb_configBackgroundColors:(NSDictionary *)backgroundColors; 63 | 64 | /** key:需要将UIControlState枚举包装为NSNumber类型即可 */ 65 | - (void)hcb_configTitleLabelFont:(NSDictionary *)titleLabelFonts; 66 | 67 | 68 | /** 切换按钮状态时,执行动画的时间,默认0.25s(只有动画执行完毕后,才能会执行下一个点击事件) */ 69 | @property (nonatomic, assign) NSTimeInterval hcb_animatedDuration; 70 | 71 | @end 72 | 73 | NS_ASSUME_NONNULL_END 74 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/UIButton+State/UIButton+HCBState.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIButton+HCBState.m 3 | // ToolKit 4 | // 5 | // Created by wangshuaipeng on 16/9/9. 6 | // gitHub https://github.com/spWang/UIButton-State 7 | // Copyright © 2016年 Mac-pro. All rights reserved. 8 | // 9 | 10 | #define objc_setObjRETAIN(key,value) objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC) 11 | 12 | #define objc_getObj(key) objc_getAssociatedObject(self, key) 13 | 14 | #define objc_exchangeMethodAToB(methodA,methodB) method_exchangeImplementations(class_getInstanceMethod([self class], methodA),class_getInstanceMethod([self class], methodB)); 15 | 16 | #define backgroundColorKEY(state) [NSString stringWithFormat:@"backgroundColor%zd",state] 17 | #define borderColorKEY(state) [NSString stringWithFormat:@"borderColor%zd",state] 18 | #define borderWidthKEY(state) [NSString stringWithFormat:@"borderWidth%zd",state] 19 | 20 | #import "UIButton+HCBState.h" 21 | #import 22 | 23 | 24 | //Model 25 | @interface HCBPropertyModel : NSObject 26 | @property (nonatomic, assign) UIControlState state; 27 | @property (nonatomic, copy) NSString *keyPath; 28 | @property (nonatomic, weak) id value; 29 | + (instancetype)propertyModelWithValue:(nullable id)value keyPath:(NSString *)keyPath state:(UIControlState)state; 30 | @end 31 | 32 | @implementation HCBPropertyModel 33 | + (instancetype)propertyModelWithValue:(id)value keyPath:(NSString *)keyPath state:(UIControlState)state { 34 | HCBPropertyModel *model = [HCBPropertyModel new]; 35 | model.value = value; 36 | model.keyPath = keyPath; 37 | model.state = state; 38 | return model; 39 | } 40 | @end 41 | 42 | @interface UIButton () 43 | @property (nonatomic, strong) NSMutableDictionary *animates; 44 | @property (nonatomic, strong) NSMutableDictionary *borderColors; 45 | @property (nonatomic, strong) NSMutableDictionary *borderWidths; 46 | @property (nonatomic, strong) NSMutableDictionary *backgroundColors; 47 | @property (nonatomic, strong) NSMutableDictionary *titleLabelFonts; 48 | @property (nonatomic, strong) NSMutableArray *subViewPropertyArr; 49 | @property (nonatomic, assign) NSInteger subViewTag; 50 | 51 | @end 52 | @implementation UIButton (HCBState) 53 | #pragma mark - lefe cycle 54 | + (void)load { 55 | objc_exchangeMethodAToB(@selector(setHighlighted:), 56 | @selector(hcb_setHighlighted:)); 57 | objc_exchangeMethodAToB(@selector(setEnabled:), 58 | @selector(hcb_setEnabled:)); 59 | objc_exchangeMethodAToB(@selector(setSelected:), 60 | @selector(hcb_setSelected:)); 61 | } 62 | 63 | #pragma mark - public method 64 | - (nullable UIColor *)hcb_currentBorderColor { 65 | UIColor *color = [self hcb_borderColorForState:self.state]; 66 | if (!color) { 67 | color = [UIColor colorWithCGColor:self.layer.borderColor]; 68 | } 69 | return color; 70 | } 71 | 72 | - (CGFloat)hcb_currentBorderWidth { 73 | CGFloat width = [self hcb_borderWidthForState:self.state-1]; 74 | if (!width) { 75 | width = self.layer.borderWidth; 76 | } 77 | return width; 78 | } 79 | 80 | - (nullable UIColor *)hcb_currentBackgroundColor { 81 | UIColor *color = [self hcb_backgroundColorForState:self.state]; 82 | if (!color) { 83 | color = self.backgroundColor; 84 | } 85 | return color; 86 | } 87 | 88 | - (UIFont *)hcb_currentTitleLabelFont { 89 | UIFont *font = [self hcb_titleLabelFontForState:(self.state-1)]; 90 | if (!font) { 91 | font = self.titleLabel.font; 92 | } 93 | return font; 94 | } 95 | 96 | 97 | - (void)hcb_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state animated:(BOOL)animated { 98 | if (backgroundColor) { 99 | [self.backgroundColors setObject:backgroundColor forKey:@(state)]; 100 | [self.animates setObject:@(animated) forKey:backgroundColorKEY(state)]; 101 | } 102 | if(self.state == state) { 103 | self.backgroundColor = backgroundColor; 104 | } 105 | } 106 | 107 | - (void)hcb_setborderColor:(UIColor *)borderColor forState:(UIControlState)state animated:(BOOL)animated { 108 | if (borderColor) { 109 | [self.borderColors setObject:borderColor forKey:@(state)]; 110 | [self.animates setObject:@(animated) forKey:borderColorKEY(state)]; 111 | } 112 | if(self.state == state) { 113 | self.layer.borderColor = borderColor.CGColor; 114 | } 115 | } 116 | 117 | - (void)hcb_setborderWidth:(CGFloat)borderWidth forState:(UIControlState)state animated:(BOOL)animated { 118 | 119 | [self.borderWidths setObject:@(borderWidth) forKey:@(state)]; 120 | [self.animates setObject:@(animated) forKey:borderWidthKEY(state)]; 121 | 122 | if(self.state == state) { 123 | self.layer.borderWidth = borderWidth; 124 | } 125 | } 126 | 127 | - (void)hcb_setTitleLabelFont:(UIFont *)titleLabelFont forState:(UIControlState)state { 128 | if (titleLabelFont) { 129 | [self.titleLabelFonts setObject:titleLabelFont forKey:@(state)]; 130 | } 131 | if(self.state == state) { 132 | self.titleLabel.font = titleLabelFont; 133 | } 134 | } 135 | 136 | - (nullable UIColor *)hcb_borderColorForState:(UIControlState)state { 137 | return [self.borderColors objectForKey:@(state)]; 138 | } 139 | 140 | - (CGFloat)hcb_borderWidthForState:(UIControlState)state { 141 | #if defined(__LP64__) && __LP64__ 142 | return [[self.borderWidths objectForKey:@(state)] doubleValue]; 143 | #else 144 | return [[self.borderWidths objectForKey:@(state)] floatValue]; 145 | #endif 146 | } 147 | 148 | - (nullable UIColor *)hcb_backgroundColorForState:(UIControlState)state { 149 | return [self.backgroundColors objectForKey:@(state)]; 150 | } 151 | 152 | - (UIFont *)hcb_titleLabelFontForState:(UIControlState)state { 153 | return [self.titleLabelFonts objectForKey:@(state)]; 154 | } 155 | 156 | - (void)hcb_configBorderColors:(NSDictionary *)borderColors { 157 | self.borderColors = [borderColors mutableCopy]; 158 | [borderColors enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, UIColor * _Nonnull obj, BOOL * _Nonnull stop) { 159 | [self.animates setObject:@(NO) forKey:borderColorKEY(key.integerValue)]; 160 | }]; 161 | [self updateButton]; 162 | } 163 | 164 | - (void)hcb_configBackgroundColors:(NSDictionary *)backgroundColors { 165 | self.backgroundColors = [backgroundColors mutableCopy]; 166 | [backgroundColors enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, UIColor * _Nonnull obj, BOOL * _Nonnull stop) { 167 | [self.animates setObject:@(NO) forKey:backgroundColorKEY(key.integerValue)]; 168 | }]; 169 | [self updateButton]; 170 | } 171 | 172 | - (void)hcb_configTitleLabelFont:(NSDictionary *)titleLabelFonts { 173 | self.titleLabelFonts = [titleLabelFonts mutableCopy]; 174 | [self updateButton]; 175 | } 176 | 177 | - (void)hcb_setSubViewValue:(nullable id)value forKeyPath:(NSString *)keyPath forState:(UIControlState)state withSubViewTag:(NSInteger)tag { 178 | UIView *subView = [self viewWithTag:tag]; 179 | self.subViewTag = tag; 180 | if (keyPath) { 181 | //不能为空 182 | id nonNullValue = value ? value : [NSNull null]; 183 | HCBPropertyModel *model = [HCBPropertyModel propertyModelWithValue:nonNullValue keyPath:keyPath state:state]; 184 | [self.subViewPropertyArr addObject:model]; 185 | } 186 | 187 | [self.subViewPropertyArr enumerateObjectsUsingBlock:^(HCBPropertyModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) { 188 | if (self.state == model.state) { 189 | //可以为空 190 | id nullableValue = (model.value == [NSNull null]) ? nil : model.value; 191 | [subView setValue:nullableValue forKeyPath:model.keyPath]; 192 | } 193 | }]; 194 | 195 | } 196 | 197 | #pragma mark - override 198 | - (void)hcb_setSelected:(BOOL)selected { 199 | [self hcb_setSelected:selected]; 200 | [self updateButton]; 201 | } 202 | 203 | - (void)hcb_setEnabled:(BOOL)enabled { 204 | [self hcb_setEnabled:enabled]; 205 | [self updateButton]; 206 | } 207 | 208 | - (void)hcb_setHighlighted:(BOOL)highlighted { 209 | [self hcb_setHighlighted:highlighted]; 210 | [self updateButton]; 211 | } 212 | 213 | #pragma mark - private method 214 | - (void)updateButton { 215 | //updateBackgroundColor 216 | UIColor *backgroundColor = [self hcb_backgroundColorForState:self.state]; 217 | if (backgroundColor) { 218 | [self updateBackgroundColor:backgroundColor]; 219 | } else { 220 | UIColor *normalColor = [self hcb_backgroundColorForState:UIControlStateNormal]; 221 | if (normalColor) { 222 | [self updateBackgroundColor:normalColor]; 223 | } 224 | } 225 | 226 | //updateBorderColor 227 | UIColor *borderColor = [self hcb_borderColorForState:self.state]; 228 | if (borderColor) { 229 | [self updateBorderColor:borderColor]; 230 | } else { 231 | UIColor *normalColor = [self hcb_borderColorForState:UIControlStateNormal]; 232 | if (normalColor) { 233 | [self updateBorderColor:normalColor]; 234 | } 235 | } 236 | 237 | //updateBorderWidth 238 | CGFloat borderWidth = [self hcb_borderWidthForState:self.state]; 239 | [self updateBorderWidth:borderWidth]; 240 | 241 | //updateTitleLabelFont 242 | UIFont *titleLabelFont = [self hcb_titleLabelFontForState:self.state]; 243 | if (titleLabelFont) { 244 | self.titleLabel.font = titleLabelFont; 245 | } else { 246 | UIFont *normalFont = [self hcb_titleLabelFontForState:UIControlStateNormal]; 247 | if (normalFont) { 248 | self.titleLabel.font = normalFont; 249 | } 250 | } 251 | 252 | //updateSubViewProperty 253 | UIView *subView = [self viewWithTag:self.subViewTag]; 254 | if (subView && self.subViewPropertyArr.count>0) { 255 | [self.subViewPropertyArr enumerateObjectsUsingBlock:^(HCBPropertyModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) { 256 | //点击一次,方法多次调用,model.value可能为nil,此时不应进入赋值,否则覆盖掉之前的值 257 | if (self.state == model.state && model.value) { 258 | //转换成nil 259 | id nullableValue = (model.value == [NSNull null]) ? nil : model.value; 260 | [subView setValue:nullableValue forKeyPath:model.keyPath]; 261 | } 262 | }]; 263 | } 264 | } 265 | 266 | - (void)updateBackgroundColor:(UIColor *)backgroundColor { 267 | NSNumber *animateValue = [self.animates objectForKey:backgroundColorKEY(self.state)]; 268 | if (!animateValue) return;//不存在 269 | 270 | if (animateValue.integerValue == self.subViewTag) { 271 | self.backgroundColor = backgroundColor; 272 | }else {//等于1 273 | [UIView animateWithDuration:self.hcb_animatedDuration animations:^{ 274 | self.backgroundColor = backgroundColor; 275 | }]; 276 | } 277 | } 278 | 279 | - (void)updateBorderColor:(UIColor *)borderColor { 280 | NSNumber *animateValue = [self.animates objectForKey:borderColorKEY(self.state)]; 281 | if (!animateValue) return;//不存在 282 | 283 | if (animateValue.integerValue == 0) { 284 | self.layer.borderColor = borderColor.CGColor; 285 | [self.layer removeAnimationForKey:@"borderColorAnimationKey"]; 286 | }else {//等于1 287 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"borderColor"]; 288 | animation.fromValue = (__bridge id _Nullable)(self.layer.borderColor); 289 | animation.toValue = (__bridge id _Nullable)(borderColor.CGColor); 290 | animation.duration = self.hcb_animatedDuration; 291 | animation.removedOnCompletion = NO; 292 | animation.fillMode = kCAFillModeForwards; 293 | [self.layer addAnimation:animation forKey:@"borderColorAnimationKey"]; 294 | self.layer.borderColor = borderColor.CGColor; 295 | } 296 | } 297 | 298 | - (void)updateBorderWidth:(CGFloat)borderWidth { 299 | NSNumber *animateValue = [self.animates objectForKey:borderWidthKEY(self.state)]; 300 | if (!animateValue) return;//不存在 301 | 302 | if (animateValue.integerValue == 0) { 303 | self.layer.borderWidth = borderWidth; 304 | [self.layer removeAnimationForKey:@"borderWidthAnimationKey"]; 305 | }else {//等于1 306 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"borderWidth"]; 307 | animation.fromValue = @(self.layer.borderWidth); 308 | animation.toValue = @(borderWidth); 309 | animation.duration = self.hcb_animatedDuration; 310 | animation.removedOnCompletion = NO; 311 | animation.fillMode = kCAFillModeForwards; 312 | [self.layer addAnimation:animation forKey:@"borderWidthAnimationKey"]; 313 | self.layer.borderWidth = borderWidth; 314 | } 315 | } 316 | 317 | #pragma mark - getters and setters 318 | - (void)setAnimates:(NSMutableDictionary *)animates { 319 | objc_setObjRETAIN(@selector(animates), animates); 320 | } 321 | 322 | - (NSMutableDictionary *)animates { 323 | NSMutableDictionary *animates = objc_getObj(@selector(animates)); 324 | if (!animates) { 325 | animates = [NSMutableDictionary new]; 326 | self.animates = animates; 327 | } 328 | return animates; 329 | } 330 | 331 | - (void)setBorderColors:(NSMutableDictionary *)borderColors { 332 | objc_setObjRETAIN(@selector(borderColors), borderColors); 333 | } 334 | 335 | - (NSMutableDictionary *)borderColors { 336 | NSMutableDictionary *borderColors = objc_getObj(@selector(borderColors)); 337 | if (!borderColors) { 338 | borderColors = [NSMutableDictionary new]; 339 | self.borderColors = borderColors; 340 | } 341 | return borderColors; 342 | } 343 | 344 | - (void)setBorderWidths:(NSMutableDictionary *)borderWidths { 345 | objc_setObjRETAIN(@selector(borderWidths), borderWidths); 346 | 347 | } 348 | - (NSMutableDictionary *)borderWidths { 349 | 350 | NSMutableDictionary *borderWidths = objc_getObj(@selector(borderWidths)); 351 | if (!borderWidths) { 352 | borderWidths = [NSMutableDictionary new]; 353 | self.borderWidths = borderWidths; 354 | } 355 | return borderWidths; 356 | } 357 | 358 | - (void)setBackgroundColors:(NSMutableDictionary *)backgroundColors { 359 | objc_setObjRETAIN(@selector(backgroundColors), backgroundColors); 360 | } 361 | 362 | - (NSMutableDictionary *)backgroundColors { 363 | NSMutableDictionary *backgroundColors = objc_getObj(@selector(backgroundColors)); 364 | if(!backgroundColors) { 365 | backgroundColors = [[NSMutableDictionary alloc] init]; 366 | self.backgroundColors = backgroundColors; 367 | } 368 | return backgroundColors; 369 | } 370 | 371 | - (void)setTitleLabelFonts:(NSMutableDictionary *)titleLabelFonts { 372 | objc_setObjRETAIN(@selector(titleLabelFonts), titleLabelFonts); 373 | } 374 | 375 | - (NSMutableDictionary *)titleLabelFonts { 376 | NSMutableDictionary *titleLabelFonts = objc_getObj(@selector(titleLabelFonts)); 377 | if(!titleLabelFonts) { 378 | titleLabelFonts = [[NSMutableDictionary alloc] init]; 379 | self.titleLabelFonts = titleLabelFonts; 380 | } 381 | return titleLabelFonts; 382 | } 383 | 384 | - (void)setHcb_animatedDuration:(NSTimeInterval)hcb_animatedDuration { 385 | objc_setObjRETAIN(@selector(hcb_animatedDuration), @(hcb_animatedDuration)); 386 | } 387 | 388 | - (NSTimeInterval)hcb_animatedDuration { 389 | NSTimeInterval duartion = [objc_getObj(@selector(hcb_animatedDuration)) doubleValue]; 390 | if (duartion == 0) { 391 | duartion = 0.25; 392 | } 393 | return duartion; 394 | } 395 | 396 | - (void)setSubViewPropertyArr:(NSMutableArray *)subViewPropertyArr { 397 | objc_setObjRETAIN(@selector(subViewPropertyArr), subViewPropertyArr); 398 | } 399 | 400 | - (NSMutableArray *)subViewPropertyArr { 401 | NSMutableArray *subViewPropertyArr = objc_getObj(@selector(subViewPropertyArr)); 402 | if(!subViewPropertyArr) { 403 | subViewPropertyArr = [[NSMutableArray alloc] init]; 404 | self.subViewPropertyArr = subViewPropertyArr; 405 | } 406 | return subViewPropertyArr; 407 | } 408 | 409 | -(void)setSubViewTag:(NSInteger)subViewTag { 410 | objc_setObjRETAIN(@selector(subViewTag), @(subViewTag)); 411 | } 412 | 413 | - (NSInteger)subViewTag { 414 | return [objc_getObj(@selector(subViewTag)) integerValue]; 415 | } 416 | @end 417 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/UIColor+Hex.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+Hex.h 3 | // ToolKit 4 | // 5 | // Created by wangshuaipeng on 16/9/2. 6 | // Copyright © 2016年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIColor (Hex) 12 | 13 | ///支持 0X123456、0x123456(效率高) 14 | + (instancetype)hcb_colorWithHex:(u_int32_t)hex; 15 | 16 | ///支持 0X123456、0x123456(效率高) 17 | + (instancetype)hcb_colorWithHex:(u_int32_t)hex alpha:(CGFloat)alpha; 18 | 19 | ///从十六进制字符串获取颜色,color:支持@“#123456”、 @“0X123456”、@“0x123456”、 @“123456”四种格式 20 | + (instancetype)hcb_colorWithHexString:(NSString *)color; 21 | 22 | ///从十六进制字符串获取颜色,color:支持@“#123456”、 @“0X123456”、@“0x123456”、 @“123456”四种格式 23 | + (instancetype)hcb_colorWithHexString:(NSString *)color alpha:(CGFloat)alpha; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/UIColor+Hex.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIColor+Hex.m 3 | // ToolKit 4 | // 5 | // Created by wangshuaipeng on 16/9/2. 6 | // Copyright © 2016年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #import "UIColor+Hex.h" 10 | 11 | @implementation UIColor (Hex) 12 | 13 | + (instancetype)hcb_colorWithHex:(u_int32_t)hex { 14 | return [self hcb_colorWithHex:hex alpha:1.0f]; 15 | } 16 | 17 | + (instancetype)hcb_colorWithHex:(u_int32_t)hex alpha:(CGFloat)alpha { 18 | int red = (hex & 0xFF0000) >> 16; 19 | int green = (hex & 0x00FF00) >> 8; 20 | int blue = hex & 0x0000FF; 21 | return [self colorWithR:red / 255.0 g:green / 255.0 b:blue / 255.0 alpha:alpha]; 22 | } 23 | 24 | + (UIColor *)hcb_colorWithHexString:(NSString *)color alpha:(CGFloat)alpha { 25 | //删除字符串中的空格 26 | NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; 27 | // String should be 6 or 8 characters 28 | if ([cString length] <3 || [cString length] > 8) { 29 | return [UIColor clearColor]; 30 | } 31 | // strip 0X if it appears 32 | //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾 33 | if ([cString hasPrefix:@"0X"]||[cString hasPrefix:@"0x"]) { 34 | cString = [cString substringFromIndex:2]; 35 | } 36 | //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾 37 | if ([cString hasPrefix:@"#"]) { 38 | cString = [cString substringFromIndex:1]; 39 | } 40 | if (!([cString length] == 6 || [cString length] ==3)) { 41 | return [UIColor clearColor]; 42 | } 43 | 44 | if ([cString length] == 6) { 45 | // Separate into r, g, b substrings 46 | NSRange range; 47 | range.location = 0; 48 | range.length = 2; 49 | //r 50 | NSString *rString = [cString substringWithRange:range]; 51 | //g 52 | range.location = 2; 53 | NSString *gString = [cString substringWithRange:range]; 54 | //b 55 | range.location = 4; 56 | NSString *bString = [cString substringWithRange:range]; 57 | 58 | // Scan values 59 | unsigned int r, g, b; 60 | [[NSScanner scannerWithString:rString] scanHexInt:&r]; 61 | [[NSScanner scannerWithString:gString] scanHexInt:&g]; 62 | [[NSScanner scannerWithString:bString] scanHexInt:&b]; 63 | return [self colorWithR:((float)r / 255.0f) g:((float)g / 255.0f) b:((float)b / 255.0f) alpha:alpha]; 64 | }else{ 65 | // Separate into r, g, b substrings 66 | NSRange range; 67 | range.location = 0; 68 | range.length = 1; 69 | //r 70 | NSString *rString = [cString substringWithRange:range]; 71 | rString = [NSString stringWithFormat:@"%@%@",rString,rString]; 72 | //g 73 | range.location = 1; 74 | NSString *gString = [cString substringWithRange:range]; 75 | gString = [NSString stringWithFormat:@"%@%@",gString,gString]; 76 | //b 77 | range.location = 2; 78 | NSString *bString = [cString substringWithRange:range]; 79 | bString = [NSString stringWithFormat:@"%@%@",bString,bString]; 80 | 81 | // Scan values 82 | unsigned int r, g, b; 83 | [[NSScanner scannerWithString:rString] scanHexInt:&r]; 84 | [[NSScanner scannerWithString:gString] scanHexInt:&g]; 85 | [[NSScanner scannerWithString:bString] scanHexInt:&b]; 86 | return [self colorWithR:((float)r / 255.0f) g:((float)g / 255.0f) b:((float)b / 255.0f) alpha:alpha]; 87 | } 88 | } 89 | 90 | //默认alpha值为1 91 | + (UIColor *)hcb_colorWithHexString:(NSString *)color { 92 | return [self hcb_colorWithHexString:color alpha:1.0f]; 93 | } 94 | 95 | 96 | + (instancetype)colorWithR:(CGFloat)red g:(CGFloat)green b:(CGFloat)blue alpha:(CGFloat)alpha { 97 | if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) { 98 | return [UIColor colorWithDisplayP3Red:red green:green blue:blue alpha:alpha]; 99 | } 100 | return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 101 | } 102 | 103 | 104 | @end 105 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 16/10/14. 6 | // gitHub https://github.com/spWang/UIButton-State 7 | // Copyright © 2016年 Mac-pro. All rights reserved. 8 | // 9 | 10 | #import 11 | 12 | @interface ViewController : UIViewController 13 | 14 | 15 | @end 16 | 17 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 16/10/14. 6 | // gitHub https://github.com/spWang/UIButton-State 7 | // Copyright © 2016年 Mac-pro. All rights reserved. 8 | // 9 | 10 | #import "ViewController.h" 11 | #import "UIButton+HCBState.h" 12 | #import "ColorConfig.h" 13 | 14 | #define ScreenWidth [[UIScreen mainScreen] bounds].size.width 15 | 16 | 17 | @interface ViewController () 18 | 19 | @end 20 | 21 | @implementation ViewController 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | 26 | [self setUPButtons]; 27 | } 28 | 29 | - (void)setUPButtons { 30 | 31 | NSInteger Kcol = 3; 32 | CGFloat margin = 10; 33 | 34 | //多个按钮 35 | for (int i =0; i<11;i++) { 36 | UIButton *button =[self hcb_button]; 37 | [self.view addSubview:button]; 38 | button.tag = i; 39 | 40 | //frame 41 | int col = i%Kcol; 42 | int row = i/Kcol; 43 | 44 | CGFloat btnW = (ScreenWidth-(Kcol+1)*margin)/Kcol; 45 | CGFloat btnH = btnW; 46 | CGFloat btnX = margin +col * (margin +btnW); 47 | CGFloat btnY = margin + row* (margin+btnH); 48 | 49 | button.frame = CGRectMake(btnX, btnY+10, btnW, btnH); 50 | 51 | //buttonSubView 52 | if (i == 5) { 53 | UILabel *subLabel = [self hcb_label]; 54 | [button addSubview:subLabel]; 55 | subLabel.frame = CGRectMake(10, 10, button.bounds.size.width-20, button.bounds.size.height/3); 56 | } 57 | 58 | [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 59 | 60 | //configButton 61 | [self configButton:button]; 62 | 63 | 64 | } 65 | } 66 | 67 | - (void)configButton:(UIButton *)button { 68 | switch (button.tag) { 69 | case 0: { 70 | [button hcb_setBackgroundColor:color_red forState:UIControlStateNormal animated:YES]; 71 | [button hcb_setBackgroundColor:color_skyblue forState:UIControlStateSelected animated:YES]; 72 | [button setTitle:@"背景颜色有动画" forState:UIControlStateNormal]; 73 | button.hcb_animatedDuration = 0.5; 74 | } 75 | break; 76 | 77 | case 1: { 78 | [button hcb_setBackgroundColor:color_red forState:UIControlStateNormal animated:NO]; 79 | [button hcb_setBackgroundColor:color_skyblue forState:UIControlStateSelected animated:NO]; 80 | [button setTitle:@"背景颜色无动画" forState:UIControlStateNormal]; 81 | 82 | } 83 | break; 84 | 85 | case 2: { 86 | button.layer.borderWidth = 10; 87 | [button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:YES]; 88 | [button hcb_setborderColor:color_yellow forState:UIControlStateSelected animated:YES]; 89 | [button setTitle:@"borderColor有动画" forState:UIControlStateNormal]; 90 | } 91 | break; 92 | 93 | case 3: { 94 | button.layer.borderWidth = 10; 95 | [button hcb_setborderColor:[UIColor purpleColor] forState:UIControlStateNormal animated:NO]; 96 | [button hcb_setborderColor:color_yellow forState:UIControlStateSelected animated:NO]; 97 | [button setTitle:@"borderColor无动画" forState:UIControlStateNormal]; 98 | } 99 | break; 100 | 101 | case 4: { 102 | [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:10] forState:UIControlStateNormal]; 103 | [button hcb_setTitleLabelFont:[UIFont systemFontOfSize:16] forState:UIControlStateSelected]; 104 | [button setTitle:@"标题font" forState:UIControlStateNormal]; 105 | } 106 | break; 107 | 108 | case 5: { 109 | [button hcb_setSubViewValue:@(NSTextAlignmentLeft) forKeyPath:@"textAlignment" forState:UIControlStateNormal withSubViewTag:10001]; 110 | [button hcb_setSubViewValue:@(NSTextAlignmentRight) forKeyPath:@"textAlignment" forState:UIControlStateSelected withSubViewTag:10001]; 111 | [button hcb_setSubViewValue:@"子View(左)" forKeyPath:@"text" forState:UIControlStateNormal withSubViewTag:10001]; 112 | [button hcb_setSubViewValue:@"子View(右)" forKeyPath:@"text" forState:UIControlStateSelected withSubViewTag:10001]; 113 | [button hcb_setSubViewValue:color_skyblue forKeyPath:@"backgroundColor" forState:UIControlStateNormal withSubViewTag:10001]; 114 | [button hcb_setSubViewValue:nil forKeyPath:@"backgroundColor" forState:UIControlStateSelected withSubViewTag:10001]; 115 | } 116 | break; 117 | case 6: { 118 | button.layer.borderColor = color_red.CGColor; 119 | [button hcb_setborderWidth:3 forState:UIControlStateNormal animated:YES]; 120 | [button hcb_setborderWidth:20 forState:UIControlStateSelected animated:YES]; 121 | [button setTitle:@"borderWidth有动画" forState:UIControlStateNormal]; 122 | } 123 | break; 124 | 125 | case 7: { 126 | button.layer.borderColor = color_red.CGColor; 127 | [button hcb_setborderWidth:3 forState:UIControlStateNormal animated:NO]; 128 | [button hcb_setborderWidth:20 forState:UIControlStateSelected animated:NO]; 129 | [button setTitle:@"borderWidth无动画" forState:UIControlStateNormal]; 130 | } 131 | break; 132 | 133 | case 8: { 134 | [button hcb_configBackgroundColors:@{@(UIControlStateNormal) : color_red, @(UIControlStateSelected) : color_skyblue}]; 135 | [button setTitle:@"key-value方式配置" forState:UIControlStateNormal]; 136 | } 137 | break; 138 | 139 | case 9: { 140 | button.layer.borderWidth = 10; 141 | [button hcb_configBorderColors:@{@(UIControlStateNormal) : [UIColor purpleColor], @(UIControlStateSelected) : color_yellow}]; 142 | [button setTitle:@"key-value方式配置" forState:UIControlStateNormal]; 143 | } 144 | break; 145 | 146 | case 10: { 147 | [button hcb_configTitleLabelFont:@{@(UIControlStateNormal) : [UIFont systemFontOfSize:10], @(UIControlStateSelected) : [UIFont systemFontOfSize:16]}]; 148 | [button setTitle:@"key-value方式配置" forState:UIControlStateNormal]; 149 | } 150 | break; 151 | 152 | 153 | default: 154 | break; 155 | } 156 | 157 | } 158 | 159 | #pragma mark - action 160 | - (void)buttonClick:(UIButton *)button { 161 | button.selected = !button.isSelected; 162 | } 163 | 164 | #pragma mark - getters and setters 165 | - (UILabel *)hcb_label { 166 | UILabel *lbl = [[UILabel alloc]init]; 167 | lbl.text = @"子View"; 168 | lbl.font = [UIFont systemFontOfSize:10]; 169 | lbl.textAlignment = NSTextAlignmentCenter; 170 | lbl.backgroundColor = color_black; 171 | lbl.textColor = [UIColor whiteColor]; 172 | lbl.layer.masksToBounds = YES; 173 | lbl.layer.cornerRadius = 3; 174 | lbl.tag = 10001; 175 | return lbl; 176 | } 177 | 178 | - (UIButton *)hcb_button { 179 | UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom]; 180 | button.backgroundColor = color_lightGray; 181 | button.titleLabel.font = [UIFont systemFontOfSize:11]; 182 | button.layer.masksToBounds = YES; 183 | button.layer.cornerRadius = 5; 184 | return button; 185 | } 186 | 187 | 188 | @end 189 | -------------------------------------------------------------------------------- /UIButtonState/UIButtonState/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // UIButtonState 4 | // 5 | // Created by wangshuaipeng on 16/10/14. 6 | // Copyright © 2016年 Mac-pro. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spWang/UIButton-State/8cad9cb584273837726cd4cea52580cd6c4a6bb5/demo.gif --------------------------------------------------------------------------------