IBTCustomButtom.m 4.49 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
//
//  IBTCustomButtom.m
//  XFFruit
//
//  Created by Xummer on 4/13/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "IBTCustomButtom.h"

@implementation IBTCustomButtom

+ (IBTUIView *)buttonViewWithTitle:(NSString *)title
                             color:(UIColor *)color
                            topGap:(CGFloat)fTopGap
                    containerWidth:(CGFloat)fWidth
                           pointer:(UIButton * __autoreleasing *)buttonPointer
                            target:(id)target
                            action:(SEL)selector
{
    CGFloat fPadding = IBT_GROUP_CELL_LEFT_PADDING;
    
    CGRect rect = CGRectMake(0, 0, fWidth, 0);
    rect.size.height = IBT_GROUP_CELL_BUTTON_HEIGHT + fPadding + (fTopGap > 0 ? fTopGap : fPadding);
    IBTUIView *view = [[IBTUIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor clearColor];
    
    rect = view.bounds;
    if (fTopGap > 0) {
        rect.origin.y = fTopGap - fPadding;
        rect.size.height -= CGRectGetMinY(rect);
    }
    
    UIButton *button =
    [[self class] buttonWithTitle:title color:color target:target action:selector];
    button.frame = CGRectInset(rect, fPadding, fPadding);

    [view addSubview:button];
    
    if (buttonPointer) {
        *buttonPointer = button;
    }
    
    return view;
}

+ (UIButton *)buttonWithTitle:(NSString *)title
                        color:(UIColor *)color
                       target:(id)target
                       action:(SEL)selector
{
    IBTCustomButtom *button = [IBTCustomButtom buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = IBT_BTN_CORNER_RADIUS;
    button.layer.masksToBounds = YES;
    [button setBackgroundImage:[UIImage imageWithColor:color ? : ICR_TINTCOLOR]
                      forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageWithColor:ICR_DISABLE_BTN_COLOR]
                      forState:UIControlStateDisabled];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

陈俊俊's avatar
陈俊俊 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

+ (UIButton *)creatButtonWithFrame:(CGRect)frame target:(id)target sel:(SEL)sel tag:(NSInteger)tag image:(NSString *)name title:(NSString *)title titleColor:(UIColor *)titleCorlor isCorner:(BOOL)isCornor corner:(CGFloat)corner bgColor:(UIColor *)bgcolor{
    
    UIButton *button = nil;
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (name) {//创建图片按钮
        [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
        if (title) {
            //创建 图片 和 标题按钮
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:titleCorlor forState:UIControlStateNormal];
        }
    }else if(title){//创建标题按钮
        [button setBackgroundColor:bgcolor];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:titleCorlor forState:UIControlStateNormal];
    }
    
    button.frame = frame;
    button.tag = tag;
    if (isCornor) {
        button.layer.cornerRadius = corner;
        button.layer.masksToBounds = YES;
    }
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    return button;
}

陈俊俊's avatar
陈俊俊 committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#pragma mark - border

- (void)addTopBorderWithHeight:(CGFloat)height color:(UIColor *)color
{
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, self.frame.size.width, height);
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
}

- (void)addBottomBorderWithHeight:(CGFloat)height color:(UIColor *)color
{
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, self.frame.size.height - height, self.frame.size.width, height);
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
}

- (void)addLeftBorderWithWidth:(CGFloat)width color:(UIColor *)color
{
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(0, 0, width, self.frame.size.height);
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
}

- (void)addRightBorderWithWidth:(CGFloat)width color:(UIColor *)color
{
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(self.frame.size.width - width, 0, width, self.frame.size.height);
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
}

陈俊俊's avatar
陈俊俊 committed
129

mei's avatar
mei committed
130
@end