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. Hdfs数据备份

    Hdfs数据备份 一.概述 本文的hdfs数据备份是在两个集群之间进行的,如果使用snapshot在同一个集群上做备份,如果datanode损坏或误操作清空了数据,这样的备份就无法完全保证数据安全性. ...

  2. maven环境搭建Myeclipse配置

    一.Maven的下载安装 准备工作: 1.安装环境:windows 2.需安装JDK,并配置环境变量(略) 3.Maven版本3.0.5 4.下载地址:链接:https://pan.baidu.com ...

  3. [codeup] 2046 八皇后

    题目描述 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8 ...

  4. UIKit 框架之UIAlertController

    IOS8之后增加了UIAlertController类,它可以表示UIAlertView和UIActionSheet.它继承自UIViewController. - (BOOL)application ...

  5. [转]How can I list all foreign keys referencing a given table in SQL Server?

    本文转自:https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given- ...

  6. JavaScript学习总结(三)——闭包、IIFE、原型、函数与对象

    一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...

  7. Web前端开发工程师常用技术网站整理

    1.常用工具相关 有道云笔记 http://note.youdao.com/signIn/index.html 36镇-最好用的共享收藏夹 http://www.36zhen.com/ 浏览器同步测试 ...

  8. STS的安装教程-鹏鹏

    STS全称Spring Tools Suite. 简介:Spring Tools Suite (STS)其实就是一个被包装过的Eclipse,主要用于快速的开发Spring项目,我们不用再去编辑繁琐的 ...

  9. WinForm窗体多线程操作实例

    最近在学习C# 多线程相关知识,这块一直比较薄弱,在网上查了一下资料,学习了一下前辈们的经验,小弟自己也比葫芦画瓢的写了一个,自学一下. 代码如下 using System; using System ...

  10. python__new__与__init__的区别

    __new__ __init__区别 1 class A(object): 2 def __init__(self,*args, **kwargs): 3 print "init A&quo ...