Instantly share code, notes, and snippets.
Last active
October 22, 2017 14:51
-
Star
1
(1)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save oliverdowling/11378520 to your computer and use it in GitHub Desktop.
A custom UIButton extension that can change title and image insets depending on button state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CEButton.h | |
// | |
// Created by Cemal Eker on 11/12/13. (2013-11-12) | |
// Modified by Oliver Dowling on 2014-04-28. | |
// | |
#import <UIKit/UIKit.h> | |
@interface CEButton : UIButton | |
- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets forState:(UIControlState)state; | |
- (void)setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets forState:(UIControlState)state; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CEButton.m | |
// | |
// Created by Cemal Eker on 11/12/13. (2013-11-12) | |
// Modified by Oliver Dowling on 2014-04-28. | |
// | |
#import "CEButton.h" | |
@interface CEButton () { | |
@private | |
NSMutableDictionary *_customTitleEdgeInsets; | |
NSMutableDictionary *_customImageEdgeInsets; | |
} | |
- (void)updateInsets; | |
@end | |
@implementation CEButton | |
- (void)initCEButton | |
{ | |
if (nil != self) { | |
_customTitleEdgeInsets = [[NSMutableDictionary alloc] init]; | |
_customImageEdgeInsets = [[NSMutableDictionary alloc] init]; | |
for (NSString *keyPath in @[@"selected", @"highlighted", @"enabled"]) { | |
[self addObserver:self | |
forKeyPath:keyPath | |
options:(NSKeyValueObservingOptionNew | |
| NSKeyValueObservingOptionOld) | |
context:NULL]; | |
} | |
} | |
} | |
- (id)init { | |
self = [super init]; | |
if (nil != self) { | |
[self initCEButton]; | |
} | |
return self; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
self = [super initWithCoder:aDecoder]; | |
if (nil != self) { | |
[self initCEButton]; | |
} | |
return self; | |
} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (nil != self) { | |
[self initCEButton]; | |
} | |
return self; | |
} | |
- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets { | |
[self setTitleEdgeInsets:titleEdgeInsets forState:UIControlStateNormal]; | |
} | |
- (void)setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets { | |
[self setImageEdgeInsets:imageEdgeInsets forState:UIControlStateNormal]; | |
} | |
- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets forState:(UIControlState)state { | |
[_customTitleEdgeInsets | |
setObject:[NSValue valueWithUIEdgeInsets:titleEdgeInsets] | |
forKey:@(state)]; | |
} | |
- (void)setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets forState:(UIControlState)state { | |
[_customImageEdgeInsets | |
setObject:[NSValue valueWithUIEdgeInsets:imageEdgeInsets] | |
forKey:@(state)]; | |
} | |
#pragma mark - Private | |
- (void)setRealTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets { | |
[super setTitleEdgeInsets:titleEdgeInsets]; | |
} | |
- (void)setRealImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets { | |
[super setImageEdgeInsets:imageEdgeInsets]; | |
} | |
- (void)updateInsets { | |
[self setRealImageEdgeInsets:[self edgeInsetsFromCustomEdgeInsets:_customImageEdgeInsets | |
forState:self.state]]; | |
[self setRealTitleEdgeInsets:[self edgeInsetsFromCustomEdgeInsets:_customTitleEdgeInsets | |
forState:self.state]]; | |
} | |
- (UIEdgeInsets)edgeInsetsFromCustomEdgeInsets:(NSDictionary *)customEdgeInsets | |
forState:(UIControlState)state { | |
UIControlState suitableState = UIControlStateNormal; | |
for (NSNumber *aState in customEdgeInsets.keyEnumerator) { | |
if (aState.integerValue == (aState.integerValue & state) | |
&& suitableState < aState.integerValue) { | |
suitableState = aState.integerValue; | |
} | |
} | |
NSValue *value = [customEdgeInsets objectForKey:@(suitableState)]; | |
if (nil != value) { | |
return value.UIEdgeInsetsValue; | |
} | |
return UIEdgeInsetsZero; | |
} | |
#pragma mark - NSKeyValueObserving | |
- (void)observeValueForKeyPath:(NSString *)keyPath | |
ofObject:(id)object | |
change:(NSDictionary *)change | |
context:(void *)context { | |
[self updateInsets]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment