RDMBorderedButton
RDMBorderedButton

https://github.com/reesemclean/RDMBorderedButton
效果:

源码:
RDMBorderedButton.h + RDMBorderedButton.m
//
// RDMBorderedButton.h
// RDMBorderedButton
//
// Created by Reese McLean on 6/12/14.
// Copyright (c) 2014 Reese McLean. All rights reserved.
// #import <UIKit/UIKit.h> @interface RDMBorderedButton : UIButton ///Adjusting corner radius manually turns off automatic corner radius updating.
@property (nonatomic, assign) CGFloat cornerRadius; ///Determines whether the buttons corner radius is adjusting based on frame changes. Default = YES.
@property (nonatomic, assign) BOOL adjustsCornerRadiusBasedOnFrame; ///Approximate ratio of corner radius to smallest side of button frame. Default = 1.0/6.0.
@property (nonatomic, assign) CGFloat cornerRadiusRatioToSmallestSide; @end
//
// RDMBorderedButton.m
// RDMBorderedButton
//
// Created by Reese McLean on 6/12/14.
// Copyright (c) 2014 Reese McLean. All rights reserved.
// #import "RDMBorderedButton.h" @implementation RDMBorderedButton -(id) init {
return [self initWithFrame:CGRectZero];
} - (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self commonSetup];
}
return self;
} -(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) { NSAssert(self.buttonType == UIButtonTypeCustom, @"RDMBorderedButton's created in interface builder must be set to type custom."); [self commonSetup];
}
return self;
} -(void) commonSetup { [self setTitleColor:self.tintColor forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; _adjustsCornerRadiusBasedOnFrame = YES;
_cornerRadiusRatioToSmallestSide = 1.0/6.0;
[self adjustCornerRadius]; self.layer.cornerRadius = _cornerRadius;
self.layer.borderWidth = 1.0;
self.layer.borderColor = self.tintColor.CGColor;
} -(void) layoutSubviews {
[super layoutSubviews];
if (self.adjustsCornerRadiusBasedOnFrame) {
[self adjustCornerRadius];
}
} -(void) tintColorDidChange {
[super tintColorDidChange];
[self setTitleColor:self.tintColor forState:UIControlStateNormal];
[self updateBorderAndFill];
} -(void) adjustCornerRadius {
_cornerRadius = roundf(MIN(CGRectGetHeight(self.frame), CGRectGetWidth(self.frame)) * self.cornerRadiusRatioToSmallestSide);
self.layer.cornerRadius = _cornerRadius;
} -(void) setTitleColor:(UIColor *)color forState:(UIControlState)state { if ([[self titleColorForState:state] isEqual:color]) {
return;
} [super setTitleColor:color forState:state]; if (state == UIControlStateNormal) {
self.tintColor = color;
} [self updateBorderAndFill];
} -(void) setTintColor:(UIColor *)tintColor { if ([[self tintColor] isEqual:tintColor]) {
return;
} [super setTintColor:tintColor];
[self setTitleColor:self.tintColor forState:UIControlStateNormal];
[self updateBorderAndFill];
} -(void) setCornerRadius:(CGFloat)cornerRadius {
self.adjustsCornerRadiusBasedOnFrame = NO;
_cornerRadius = cornerRadius;
self.layer.cornerRadius = _cornerRadius;
} -(void) setEnabled:(BOOL)enabled { [super setEnabled:enabled];
[self updateBorderAndFill]; } -(void) updateBorderAndFill {
self.layer.borderColor = self.enabled ? self.tintColor.CGColor : [self titleColorForState:UIControlStateDisabled].CGColor;
self.backgroundColor = self.highlighted ? self.tintColor : [UIColor clearColor];
} -(void) setHighlighted:(BOOL)highlighted { if (self.highlighted == highlighted) {
return;
} [super setHighlighted:highlighted]; [UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.backgroundColor = highlighted ? self.tintColor : [UIColor clearColor];
}
completion:nil]; } @end
显示的代码:
//
// RootViewController.m
//
// http://home.cnblogs.com/u/YouXianMing/
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "RDMBorderedButton.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化
RDMBorderedButton *button = \
[[RDMBorderedButton alloc] initWithFrame:CGRectMake(, , , )];
button.center = self.view.center; // 设置圆角
button.cornerRadius = .f; // 设置字体
button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin"
size:.f]; // 设置标题
[button setTitle:@"YouXianMing"
forState:UIControlStateNormal]; // 设置标题颜色
[button setTitleColor:[UIColor redColor]
forState:UIControlStateNormal]; [self.view addSubview:button];
} @end
附录:
Usage
In code: Use initWithFrame to create a button and add to a subview.
Interface Builder: Add a button as usual. Set the class to RDMBorderedButton — there are some bugs with iOS 7.1 that require you to set the buttom type to Custom in Interface Builder. Also note that you can use the "User Defined Runtime Attributes" in Interface Builder to set the corner radius (key: "cornerRadius"). The example project shows this with the black and yellow buttons.
Corner Radius
By default, RDMBorderedButton will adjusts the corner radius of its border based on its frame. You can turn this off with:
button.adjustsCornerRadiusBasedOnFrame = NO; //Default is YES
You can also change the ratio of the corner radius of this automatic adjustment:
button.cornerRadiusRatioToSmallestSide = 1.0/4.0; //Default is 1.0/6.0
Note that changes to Corner Radius will not be animated. If you would like a corner radius change to be animated you will need to animate the key path using CoreAnimation. See the programatic view controller in the example project to see an example of this.
The corner radius can be adjusted manually (this turns off automatic adjustments):
//This will forward the cornerRadius to the button's layer and turn off automatic adjustments
button.cornerRadius = 6.0;
Color
The text and border color are adjusted together. For normal state they can be changed using either:
button.tintColor = [UIColor greenColor];
or:
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
Disabled state can be adjusted using:
[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; //Default is [UIColor grayColor]
The text color when highlighted should be changed using:
[self setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; //Default is [UIColor whiteColor]
RDMBorderedButton的更多相关文章
随机推荐
- test11
-Xms512m-Xmx512m-XX:PermSize=512-XX:MaxPermSize=512
- WPF设置控件获得焦点FocusManager
简单用法如下: 在父类容器中通过附加属性FocusManager.FocusedElement来绑定需要强制获得焦点的控件,用法如下: <Grid FocusManager.FocusedE ...
- HAProxy Installation and Configuration on CentOS 6.4 to Mitigate The Effects of Abusive Clients--转
ref:http://thoughts.z-dev.org/2013/05/07/haproxy-installation-and-configuration-on-centos-6-4-to-mit ...
- SpringMVC源码阅读:核心分发器DispatcherServlet
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将介绍SpringMVC的核 ...
- C#语法之Linq查询基础二
上篇C#语法之Linq查询基础一基本把Linq介绍了一下,这篇主要是列举下它的几个常见用法. 在用之前先准备些数据,新建了两个类Student.Score,并通过静态方法提供数据. using Sys ...
- c#基础学习(0708)之静态类
再静态类中,所包含的所有成员都是“静态成员” 不是所有的静态成员都必须卸载静态类中 静态成员时属于“类”的,不是属于具体“对象”的,所以访问静态成员的时候不能通过对象来访问(对象.属性名),只能通过“ ...
- c#基础学习(0703)之string.Format格式化日期
C# string.Format格式化日期 DateTime dt = ,,,,,,); string.Format("{0:y yy yyy yyyy}",dt); //17 1 ...
- jquery源码学习笔记(一)jQuery的无new构建
本人是一名.net程序员..... 你一个.net coder 看什么jQuery 源码啊? 原因吗,很简单.技多不压身吗(麻蛋,前端工作好高...羡慕). 我一直都很喜欢JavaScript,废话不 ...
- 用于深拷贝的扩展方法 C#
using System.Runtime.Serialization.Formatters.Binary; using System.IO; public static class Tool { pu ...
- [日常] Redis基本使用测试
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(list ...