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. mongodb-导出数据到csv文件或json文件

    在mongodb的bin目录下, 有一个mongoexport, 可用于数据的导出 [wenbronk@localhost bin]$ ./mongoexport --help Usage: mong ...

  2. redis-springdata-api

    使用StringRedisTempalte操作redis五种数据类型 spring-data中继承了redisTemplate, 提供redis非切片连接池 代码github地址: https://g ...

  3. 解决php中文乱码

    在文件的第一行,加入下面这一句: header("Content-Type: text/html; charset=utf8"); 然后在把文件以utf-8的格式保存起来就行了

  4. 剑指offer62:二插搜索树的第k个节点

    题目描述: 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 中序遍历 /* struct TreeNo ...

  5. unity 中的协程

    //The coroutine will continue after all Update functionshave been calledon the next frame. ; //Conti ...

  6. Func的介绍——c#封装的代理

    经常看到  Func<int, bool>...这样的写法,看到这样的就没有心思看下去了.我们学技术还是需要静下心来. 对Func<int,bool>的Func转到定义看它的解 ...

  7. 【转】到底什么时候应该用MQ

    原文地址:http://zhuanlan.51cto.com/art/201704/536407.htm 一.缘起 一切脱离业务的架构设计与新技术引入都是耍流氓. 引入一个技术之前,首先应该解答的问题 ...

  8. Django基础必备三神装(HttpResponse、render,、redirect)

    在使用三神装的时候,首先当然是得要导入它们: from django.shortcuts import HttpResponse, render, redirect 1.HttpResponse 它是 ...

  9. grafana 安装- 曲线图展示每秒新增数据量

    下载: https://dl.grafana.com/oss/release/grafana-5.4.2.windows-amd64.zip 解压就能用 添加数据源 添加查询条件 sql 模式编写查询 ...

  10. 基于Udp的五子棋对战游戏

    引言 本文主要讲述在局域网内,使用c#基于Udp协议编写一个对战的五子棋游戏.主要从Udp的使用.游戏的绘制.对战的逻辑这三个部分来讲解. 开发环境:vs2013,.Net4.0,在文章的末尾提供源代 ...