KHFlatButton

KHFlatButton-LMLPHP

https://github.com/kylehorn/KHFlatButton

效果:

KHFlatButton-LMLPHP

对于自己做demo来说,每次设置button就不用这么折腾了,几句话就行了,非常爽:)

其实可以改进的地方非常多的.

KHFlatButton-LMLPHP

源码:

KHFlatButton.h + KHFlatButton.m

//
// KHFlatButton.h
//
// Created by Kyle Horn on 10/7/13.
// Copyright (c) 2013 Kyle Horn. All rights reserved.
// #import <UIKit/UIKit.h> @interface KHFlatButton : UIButton + (KHFlatButton *)buttonWithFrame:(CGRect)frame
withTitle:(NSString *)title
backgroundColor:(UIColor *)backgroundColor
cornerRadius:(CGFloat)radius; // Factory method that returns a button with default corner radius of 3.0
+ (KHFlatButton *)buttonWithFrame:(CGRect)frame
withTitle:(NSString *)title
backgroundColor:(UIColor *)backgroundColor; + (UIBarButtonItem *)barButtonWithTitle:(NSString *)title
backgroundColor:(UIColor *)backgroundColor; @property (nonatomic) CGFloat cornerRadius; @end
//
// KHFlatButton.m
//
// Created by Kyle Horn on 10/7/13.
// Copyright (c) 2013 Kyle Horn. All rights reserved.
// #import "KHFlatButton.h"
#import <QuartzCore/QuartzCore.h> static CGFloat const kDefaultCornerRadius = 3.0;
static CGFloat const kMinimumFontSize = 14.0;
static CGFloat const kHighlightDelta = 0.2; @interface KHFlatButton()
@property (strong, nonatomic) UIColor *originalBackgroundColor;
@end @implementation KHFlatButton @dynamic cornerRadius; - (void)awakeFromNib
{
[super awakeFromNib]; self.cornerRadius = kDefaultCornerRadius; [self addTarget:self
action:@selector(wasPressed)
forControlEvents:(UIControlEventTouchDown |
UIControlEventTouchDownRepeat |
UIControlEventTouchDragInside |
UIControlEventTouchDragEnter)]; [self addTarget:self
action:@selector(endedPress)
forControlEvents:(UIControlEventTouchCancel |
UIControlEventTouchDragOutside |
UIControlEventTouchDragExit |
UIControlEventTouchUpInside |
UIControlEventTouchUpOutside)];
} - (id)initWithFrame:(CGRect)frame withBackgroundColor:(UIColor*)backgroundColor
{
return [self initWithFrame:frame
withTitle:nil
backgroundColor:backgroundColor
cornerRadius:kDefaultCornerRadius];
} - (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title backgroundColor:(UIColor*)backgroundColor cornerRadius:(CGFloat)radius;
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = backgroundColor;
self.cornerRadius = radius;
[self setTitle:title
forState:UIControlStateNormal]; CGFloat fontSize = floorf(CGRectGetHeight(self.bounds) / 2.5);
self.titleLabel.font = [UIFont boldSystemFontOfSize:MAX(kMinimumFontSize, fontSize)]; // 根据触摸的状态分离出两个方法,一个方法用于改变自身,一个方法用于还原自身
[self addTarget:self
action:@selector(wasPressed)
forControlEvents:(UIControlEventTouchDown |
UIControlEventTouchDownRepeat |
UIControlEventTouchDragInside |
UIControlEventTouchDragEnter)]; [self addTarget:self
action:@selector(endedPress)
forControlEvents:(UIControlEventTouchCancel |
UIControlEventTouchDragOutside |
UIControlEventTouchDragExit |
UIControlEventTouchUpInside |
UIControlEventTouchUpOutside)];
}
return self;
} + (KHFlatButton *)buttonWithFrame:(CGRect)frame withTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor cornerRadius:(CGFloat)radius
{
KHFlatButton *btn = [[KHFlatButton alloc] initWithFrame:frame
withTitle:title
backgroundColor:backgroundColor
cornerRadius:radius];
return btn;
} + (KHFlatButton *)buttonWithFrame:(CGRect)frame withTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor
{
KHFlatButton *btn = [[KHFlatButton alloc] initWithFrame:frame
withTitle:title
backgroundColor:backgroundColor
cornerRadius:kDefaultCornerRadius];
return btn;
} + (UIBarButtonItem *)barButtonWithTitle:(NSString *)title backgroundColor:(UIColor *)backgroundColor
{
KHFlatButton *btn = [[KHFlatButton alloc]initWithFrame:CGRectZero withTitle:title backgroundColor:backgroundColor cornerRadius:kDefaultCornerRadius]; // Add padding to button label
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
[btn sizeToFit];
CGRect frame = btn.frame;
frame.size.width += ;
btn.frame = frame;
btn.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barBtn;
} - (void)setBackgroundColor:(UIColor *)backgroundColor
{
super.backgroundColor = backgroundColor;
self.originalBackgroundColor = backgroundColor;
} - (void)wasPressed
{
CGFloat red, grn, blu, white, alpha = 0.0;
[self.originalBackgroundColor getRed:&red
green:&grn
blue:&blu
alpha:&alpha]; [self.originalBackgroundColor getWhite:&white
alpha:&alpha]; super.backgroundColor = [UIColor colorWithRed:red - kHighlightDelta
green:grn - kHighlightDelta
blue:blu - kHighlightDelta
alpha:alpha];
} - (void)endedPress
{
super.backgroundColor = self.originalBackgroundColor;
} - (void)setCornerRadius:(CGFloat)cornerRadius
{
self.layer.cornerRadius = cornerRadius;
} - (CGFloat)cornerRadius
{
return self.layer.cornerRadius;
} @end
//
// RootViewController.m
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "KHFlatButton.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor]; // 初始化按钮
KHFlatButton *button = \
[KHFlatButton buttonWithFrame:(CGRect){CGPointZero, CGSizeMake(, )}
withTitle:@"YouXianMing"
backgroundColor:[UIColor magentaColor]];
button.center = self.view.center;
[self.view addSubview:button]; // 添加事件
[button addTarget:self
action:@selector(buttonsEvent:)
forControlEvents:UIControlEventTouchUpInside];
} - (void)buttonsEvent:(KHFlatButton *)button
{
NSLog(@"%@", button);
} @end

这个地方才是我们需要学习的地方:

KHFlatButton-LMLPHP

05-11 22:16