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.2fdelay:0.0foptions:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentStateanimations:^{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(0, 0, 100, 30)];button.center             = self.view.center;// 设置圆角button.cornerRadius       = 1.f;// 设置字体button.titleLabel.font    = [UIFont fontWithName:@"HelveticaNeue-Thin"size:13.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相关推荐

最新文章

  1. 综述 | 现在是蛋白质组学数据共享和再分析的黄金时间?
  2. UCenter创始人密码正确但是登录不了
  3. js中bind、call、apply函数的用法
  4. 编写一个简单的spring MVC程序
  5. 生物效应大数据评估聚类算法的并行优化
  6. by mango怎么group_java – MongoDB中计算的group-by字段
  7. 转储sql文件_在Linux上SQL Server中更改SQL转储文件位置
  8. Spring Boot 消息
  9. C盘总是满了,不想重装系统,不想扩充,C盘瘦身彻底解决
  10. 聊聊ConcurrentHashMap
  11. C语言中表示温度符号,摄氏度符号怎么打(SCI论文中摄氏度°C符号的正确输法)...
  12. 大数据学习之Linux基础
  13. 学习大数据需要哪些数学知识?
  14. SpiderViewer - 远程桌面客户端
  15. 仿照vue实现双向数据绑定兼容IE6
  16. 打造淘宝极简包的轻量化框架
  17. 如何打开电脑的虚拟键盘
  18. 百万调音师—音频基础知识
  19. 如何选择适合自己的吉他弦(下)
  20. 正点原子嵌入式linux视频教程,正点原子嵌入式开发完整全套视频教程

热门文章

  1. mips linux gcc mingw,gcc
  2. centos安装mysql8_Docker 快速安装 Mysql
  3. mysql cluster安装配置_mysqlcluster安装与配置_MySQL
  4. HTML2em一定是32px,px,em,rem的区别
  5. pytorch nn.Conv1d
  6. javascript window location
  7. C语言 pthread_cancelpthread_detach
  8. matplotlib.lines.Line2D
  9. python os.system(cls)没反应_这真的是全宇宙最简单的Python安装方式了
  10. 指针 | golang之指针的学习