KHFlatButton

https://github.com/kylehorn/KHFlatButton

效果:

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

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

源码:

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的更多相关文章

随机推荐

  1. 机器学习中的范数规则化之L0、L1与L2范数

    今天看到一篇讲机器学习范数规则化的文章,讲得特别好,记录学习一下.原博客地址(http://blog.csdn.net/zouxy09). 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...

  2. [笔记] print函数进阶

      1.1print(values=None,sep='',end='\n',file=sys.stdout,flush=False) 参数: values:输出到控制台的string sep:设置输 ...

  3. [PY3]——logging

    logging模块的logger.handler.filter.formatter Logger记录器 提供日志接口,供应用代码使用.logger最长用的操作有两类:配置和发送日志消息.可以通过log ...

  4. 设置input标签placeholder字体颜色

    有时会遇到这样的需求,输入框的默认提示文字与用户输入的文字不同. <input className="city" placeholder="城市"/> ...

  5. 图片切换(timer控件与ImagesList结合使用)

    private void Form3_Load(object sender, EventArgs e) { //打开窗体的时候显示第一张图片 this.pictureBox1.Image = this ...

  6. 安装SQL SEVER 2017 express 轻量入门级软件 安装教程

    1. 首先 打开网址   https://www.microsoft.com/zh-tw/sql-server/sql-server-downloads     点击下载 , 下载完成之后, 点开安装 ...

  7. fzu 2154 YesOrNo

    Problem 2154 YesOrNo Accept: 14    Submit: 29Time Limit: 1000 mSec    Memory Limit : 32768 KB Proble ...

  8. socketsever模块

    TCP协议下 服务端可以同时接收多个客户端信息 import socketserver class MySocket(socketserver.BaseRequestHandler): def han ...

  9. Yii 之components

    当我们创建一个module的时候,对应的path alias就已经创建.比如我们定义了一个module: www 1 2 3 4 5 'modules'=>array(     'www'=&g ...

  10. JavaScript standard 代码规范的全文

    这是 JavaScript standard 代码规范的全文. 掌握本规范的最好方法是安装并在自己的代码中使用它. 细则 使用两个空格进行缩进. eslint: indent function hel ...