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

随机推荐

  1. springboot-6-整合jdbc

    如果有整合jpa了, 那么在dao中直接, 不需要引入依赖 @Resource private JdbcTempalte jdbcTempalte; 如果没有的话, 就先在pom.xml中加入依赖 & ...

  2. 剑指offer65:矩阵中的路径

    题目描述: 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩阵 ...

  3. java.lang.NoSuchMethodError: org.json.JSONArray.iterator()Ljava/util/Iterator 阿里云短信

    请尝试使用 <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk ...

  4. mariadb(mysql)从库relaylog损坏无法同步的处理方法

    故障说明 晚上备用服务器自动重启,收到报警,备用服务器上的mariadb从库无法去同步主库.启动mariadb后,报如下错误(重点看红色字体)  mariadb_1 | -- :: [Note] Pl ...

  5. win7怎么去除快捷方式的小箭头

    方式一. 新建一个txt文档把以下内容复制进去 在重命名001.bat 打开就行了 但要重启之后才有效 @ECHO OFF :MENU ECHO. ECHO. =-=-=-=-=请选择您要运行的项目= ...

  6. spring MethodInterceptor方法拦截

    引用别的的:https://blog.csdn.net/u010739551/article/details/47754731 最近项目里加上了用户权限,有些操作需要登录,有些操作不需要,之前做项目做 ...

  7. Storm框架:如何根据业务条件选择不同的bolt进行下发消息

    Strom框架基本概念就不提了,这里主要讲的是Stream自定义ID的消息流.默认spout.bolt都需实现接口方法declareOutputFields,代码如下: @Override publi ...

  8. MySQL 批量删除相同前缀的表

    sql 命令批量生成drop命令 需要批量删除表,而MySQL又没有提供相关的功能:一般我们建表也都会使用相同前缀,那么,在不使用工具的情况下可以选择使用sql生成批量删除命令: 如删除以 " ...

  9. SpringBoot配置文件注入值数据校验

    package com.hoje.springboot.bean; import org.springframework.beans.factory.annotation.Value; import ...

  10. Glide填坑指南

    一.前言:再优秀的开源库都有坑要填 手上的项目使用的图片加载框架是:Universal-Image-Loader+业务需要定制化的一些代码.Universal-Image-Loader 这个框架是一个 ...