Instantly share code, notes, and snippets.
Created
April 1, 2016 11:55
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save KalpeshTalkar/201ac13e45b9f4e6743da884983ceb96 to your computer and use it in GitHub Desktop.
KBorderedButton, a custom IBDesignable UIButton
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
// | |
// KBorderedButton.h | |
// | |
// Created by Kalpesh Talkar on 01/02/16. | |
// Copyright © 2016 Kalpesh. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
IB_DESIGNABLE | |
@interface KBorderedButton : UIButton | |
- (void)setCornerRadius:(CGFloat)cornerRadius; | |
- (void)setBorderWidth:(CGFloat)borderWidth; | |
- (void)setBorderColor:(UIColor *)borderColor; | |
@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
// | |
// KBorderedButton.m | |
// | |
// Created by Kalpesh Talkar on 01/02/16. | |
// Copyright © 2016 Kalpesh. All rights reserved. | |
// | |
#import "KBorderedButton.h" | |
#import "KBorderedGradientView.h" | |
@interface KBorderedButton() | |
@property (nonatomic) IBInspectable CGFloat cornerRadius; | |
@property (nonatomic) IBInspectable CGFloat borderWidth; | |
@property (nonatomic, strong) IBInspectable UIColor *borderColor; | |
@property (nonatomic, strong) KBorderedGradientView *v; | |
@property (nonatomic, strong) UIView *progressView; | |
@end | |
@implementation KBorderedButton | |
#pragma mark - Instance Methods | |
- (instancetype)initWithCoder:(NSCoder *)aDecoder { | |
self = [super initWithCoder:aDecoder]; | |
if (self) { | |
[self renderView]; | |
} | |
return self; | |
} | |
- (instancetype)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
[self defaults]; | |
[self renderView]; | |
} | |
return self; | |
} | |
- (void)drawRect:(CGRect)rect { | |
[self renderView]; | |
} | |
- (void)setNeedsLayout { | |
[super setNeedsLayout]; | |
[self setNeedsDisplay]; | |
} | |
- (void)prepareForInterfaceBuilder { | |
[self renderView]; | |
} | |
#pragma mark - Defaults | |
- (void)defaults { | |
_cornerRadius = 0.0f; | |
_borderWidth = 1.0f; | |
_borderColor = [self tintColor]; | |
[self.titleLabel setFont:[UIFont systemFontOfSize:15]]; | |
[self setTitleColor:[self tintColor] forState:UIControlStateNormal]; | |
[self setTitleColor:[self highlightedColor] forState:UIControlStateHighlighted]; | |
if ([self titleColorForState:UIControlStateNormal == nil]) { | |
[self setTitle:@"Button" forState:UIControlStateNormal]; | |
} | |
[self setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled]; | |
} | |
#pragma mark - Highlighted Color | |
- (UIColor *)highlightedColor { | |
UIColor *highlighted = [self titleColorForState:UIControlStateNormal]; | |
highlighted = [highlighted colorWithAlphaComponent:0.0]; | |
return highlighted; | |
} | |
#pragma mark - Render View | |
- (void)renderView { | |
if (_borderColor == nil) { | |
_borderColor = [self tintColor]; | |
} | |
[self.layer setCornerRadius:_cornerRadius]; | |
[self.layer setBorderWidth:_borderWidth]; | |
[self.layer setBorderColor:[_borderColor CGColor]]; | |
} | |
#pragma mark - | |
- (void)setCornerRadius:(CGFloat)cornerRadius { | |
_cornerRadius = cornerRadius; | |
[self renderView]; | |
} | |
- (void)setBorderWidth:(CGFloat)borderWidth { | |
_borderWidth = borderWidth; | |
[self renderView]; | |
} | |
- (void)setBorderColor:(UIColor *)borderColor { | |
_borderColor = borderColor; | |
[self renderView]; | |
} | |
#pragma mark - UIImage with UIColor | |
- (UIImage *)imageWithColor:(UIColor *)color { | |
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetFillColorWithColor(context, [color CGColor]); | |
CGContextFillRect(context, rect); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment