Skip to content

Instantly share code, notes, and snippets.

@pablocarrillo
Last active December 22, 2015 20:09
Show Gist options
  • Save pablocarrillo/6524554 to your computer and use it in GitHub Desktop.
Save pablocarrillo/6524554 to your computer and use it in GitHub Desktop.
This Gist work with [UIButton+AFNetworking.h]

This Gist work with [UIButton+AFNetworking.h] (https://gist.github.com/dpettigrew/2925847) a very useful category for UIButton.

And is inspired by this stackOverflow [answer] (http://stackoverflow.com/a/10255257), just need a few touches.

With this UIButton subclass you can setup buttons with cornerRadius and shadows even with downloaded images from internet.

To use this class you can set a cornerRadius inside, previous setup is called or after setup the images, set the cornerRadius and call setupView. Anyone can use this gist as he wish, see license inside.

//
// PCFancyButton.h
// RoundButton Test
//
// Created by Pablo Isidoro Carrillo Alvarez on 11/09/2013.
// Copyright (c) 2013 Pablo Isidoro Carrillo Alvarez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "UIButton+AFNetworking.h"
@interface PCFancyButton : UIButton
@property float cornerRadius;
- (void)setupView;
@end
//
// PCFancyButton.m
// RoundButton Test
//
// Created by Pablo Isidoro Carrillo Alvarez on 11/09/2013.
// Copyright (c) 2013 Pablo Isidoro Carrillo Alvarez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "PCFancyButton.h"
@implementation PCFancyButton
- (id)initWithFrame:(CGRect)frame
{
if((self = [super initWithFrame:frame])){
[self setupView];
}
return self;
}
- (void)awakeFromNib {
[self setupView];
}
# pragma mark - main
- (void)setupView
{
//Configuring round border for button images.
self.imageView.layer.cornerRadius=_cornerRadius;
self.imageView.layer.masksToBounds=YES;
//Configuring cornerRadius for the layer of the button
self.layer.cornerRadius = _cornerRadius;
//Configuring border
self.layer.borderWidth = 4.0f;
self.layer.borderColor = [UIColor colorWithRed:167.0/255.0 green:140.0/255.0 blue:98.0/255.0 alpha:1].CGColor;
//Configuring shadow
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 1;
[self clearHighlightView];
//If you want to use gradients just uncomment the line below and configure as you wish
/*
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.layer.bounds;
gradient.cornerRadius = _cornerRadius;
gradient.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:1.0f alpha:1.0f].CGColor,
(id)[UIColor colorWithWhite:1.0f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:0.0f alpha:0.0f].CGColor,
(id)[UIColor colorWithWhite:0.0f alpha:0.4f].CGColor,
nil];
float height = gradient.frame.size.height;
gradient.locations = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0f],
[NSNumber numberWithFloat:0.2*30/height],
[NSNumber numberWithFloat:1.0-0.1*30/height],
[NSNumber numberWithFloat:1.0f],
nil];
[self.layer addSublayer:gradient];
*/
}
- (void)highlightView
{
self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.layer.shadowOpacity = 0.25;
}
- (void)clearHighlightView {
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5;
}
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted) {
[self highlightView];
} else {
[self clearHighlightView];
}
[super setHighlighted:highlighted];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment