Skip to content

Instantly share code, notes, and snippets.

@engmsaleh
Forked from oliverdowling/CEButton.h
Created January 1, 2016 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engmsaleh/8aa5e69e3274503a3650 to your computer and use it in GitHub Desktop.
Save engmsaleh/8aa5e69e3274503a3650 to your computer and use it in GitHub Desktop.
A custom UIButton extension that can change title and image insets depending on button state.
//
// CEButton.h
//
// Created by Cemal Eker on 11/12/13. (2013-11-12)
// Mofidied 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
//
// CEButton.m
//
// Created by Cemal Eker on 11/12/13. (2013-11-12)
// Mofidied 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