Skip to content

Instantly share code, notes, and snippets.

@KalpeshTalkar
Last active April 1, 2016 12:00
Show Gist options
  • Save KalpeshTalkar/fe6e53306ff0da388cc74324c711fd3e to your computer and use it in GitHub Desktop.
Save KalpeshTalkar/fe6e53306ff0da388cc74324c711fd3e to your computer and use it in GitHub Desktop.
FAB, a custom IBDesignable UIButton
//
// FAB.h
//
// Created by Kalpesh Talkar on 01/04/16.
// Copyright © 2016 Kalpesh. All rights reserved.
//
#import <UIKit/UIKit.h>
// On click block
typedef void (^OnClick)();
@interface FAB : UIButton
// Init method
- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image onClick:(OnClick)action;
@end
//
// FAB.m
//
// Created by Kalpesh Talkar on 01/04/16.
// Copyright © 2016 Kalpesh. All rights reserved.
//
#import "FAB.h"
@interface FAB() {
OnClick onClick;
}
@end
@implementation FAB
#pragma mark - Instance Methods
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self renderView];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image onClick:(OnClick)action {
self = [super initWithFrame:frame];
if (self) {
onClick = action;
[self defaults];
[self renderView];
[self setImage:image forState:UIControlStateNormal];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self renderView];
}
- (void)setNeedsLayout {
[super setNeedsLayout];
[self setNeedsDisplay];
}
- (void)prepareForInterfaceBuilder {
[self renderView];
}
#pragma mark - Defaults
- (void)defaults {
[self setTintColor:[UIColor whiteColor]];
[self.titleLabel setFont:[UIFont systemFontOfSize:15]];
[self setTitleColor:[self tintColor] forState:UIControlStateNormal];
[self setTitleColor:[self highlightedColor] forState:UIControlStateHighlighted];
[self setBackgroundColor:[UIColor redColor]];
[self setBackgroundImage:[self imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
[self addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - ButtonClicked
- (void)buttonClicked:(FAB *)fab {
onClick();
}
#pragma mark - Highlighted Color
- (UIColor *)highlightedColor {
UIColor *highlighted = [self titleColorForState:UIControlStateNormal];
highlighted = [highlighted colorWithAlphaComponent:0.0];
return highlighted;
}
#pragma mark - Render View
- (void)renderView {
CGFloat size = self.frame.size.width/2;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(size, size)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
//self.layer.cornerRadius = self.frame.size.width/2;
self.layer.shadowRadius = self.frame.size.width/2;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.layer.shadowOpacity = 0.8f;
//self.layer.masksToBounds = true;
self.layer.mask = maskLayer;
}
#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