• KHFlatButton


    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 += 20;
        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(150, 50)}
                                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

    这个地方才是我们需要学习的地方:

  • 相关阅读:
    xtrabackup详细用法
    CentOS国内YUM源及配置
    CentOS7系统配置国内yum源和epel源
    Zabbix的定期备份
    Zabbix的邮件告警
    石头剪刀布---组合数取模,数论
    组合数取模介绍----Lucas定理介绍
    乘法逆元
    费马小定理
    欧几里德与扩展欧几里德算法----数论
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3798608.html
Copyright © 2020-2023  润新知