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(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]

目录
相关文章
|
3天前
|
存储 弹性计算 UED
2024年阿里云服务器价格表出炉,阿里云服务器年付61元起!
随着互联网的普及和技术的进步,无论是个人还是企业,对服务器的需求都在逐渐增加。面对市场上琳琅满目的服务器产品,如何选择一款性价比高、满足自身需求的服务器成为了许多人的难题。今天,我要向大家推荐的是阿里云2核2G3M轻量应用服务器,这款服务器不仅配置足够满足大多数小型网站和应用的需求,而且性价比极高,秒杀价年付仅需61元!
|
存储 负载均衡 监控
分布式定时任务,你了解多少?基于Quartz实现分布式定时任务解决方案!
定时任务系统在应用平台中的重要性不言而喻,特别是互联网电商、金融等行业更是离不开定时任务。在任务数量不多、执行频率不高时,单台服务器完全能够满足。但是随着业务逐渐增加,定时任务系统必须具备高可用和水平扩展的能力,单台服务器已经不能满足需求。因此需要把定时任务系统部署到集群中,实现分布式定时任务系统集群。
4145 1
分布式定时任务,你了解多少?基于Quartz实现分布式定时任务解决方案!
|
10月前
|
Java Go
Golang面试:关于内存分配、管理以及泄漏的一切
Golang面试:关于内存分配、管理以及泄漏的一切
|
8月前
|
弹性计算 缓存 开发者
阿里云通用算力型与新品经济型云服务器价格参考(ESSD Entry和ESSD云盘)
近日,阿里云调整了部分云服务器活动价格,现在购买阿里云服务器的活动价格比前两个月更低了,其中通用算力型1125.96元起,经济型e实例182.04元起,下面我们一起看下活动价格下降之后,通用算力型u1实例和经济型e实例云服务器的最新活动价格。
970 0
阿里云通用算力型与新品经济型云服务器价格参考(ESSD Entry和ESSD云盘)
|
8月前
|
存储 Cloud Native Java
聊聊 Pulsar: Pulsar 的核心概念与基础架构
聊聊 Pulsar: Pulsar 的核心概念与基础架构
1004 0
|
Android开发
关闭安卓系统导航栏右下角自动旋转按钮
关闭安卓系统导航栏右下角自动旋转按钮
256 0
|
10月前
|
开发框架 Dart 开发工具
使用Flutter开发一套可同时运行在Android和iOS平台的代码
Flutter是一种跨平台移动应用开发框架,它允许开发者使用单一代码库构建高性能、美观且可在多个平台上运行的应用程序。本文将介绍如何使用Flutter开发一套同时适用于Android和iOS平台的代码。
|
12月前
|
SQL 数据库连接 数据库
瑶池数据库SQL-问题二
简述问题二的分析思路及最后结果
|
存储 SQL 数据库
C/C++ Qt 数据库与ComBox多级联动
Qt中的SQL数据库组件可以与`ComBox`组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个`ComBox`组件中列举出该用户所维护的主机列表,又或者当用户选择省份时,自动列举出该省份下面的城市列表等。
347 0
C/C++ Qt 数据库与ComBox多级联动