UIButton+Extras.m 4.4 KB
//
//  UIButton+Extras.m
//  DreamSleep
//
//  Created by peter on 2022/4/12.
//

#import "UIButton+Extras.h"
#import <objc/runtime.h>

@implementation UIButton (Extras)

+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor imgName:(NSString *)imgName font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

+ (instancetype)dkBtnWithSubTitle:(NSString *)subTitle imgName:(NSString *)imgName font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:subTitle forState:UIControlStateNormal];
    [btn dk_setTitleColorPicker:DKColorPickerWithKey(SubTEXT) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

+ (instancetype)dkBtnTitle:(NSString *)title imgName:(NSString *)imgName font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

+ (instancetype)dkBtnTitle:(NSString *)title font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn dk_setTitleColorPicker:DKColorPickerWithKey(TEXT) forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

- (void)adjustLayoutWithType:(UIButtonLayoutType)type midSpace:(CGFloat)midSpace sizeToFit:(BOOL)sizeToFit
{
    if (sizeToFit) {
        [self sizeToFit];
        if (UIButtonLayoutTypeLeftTitleRightImage == type) {
            self.width += midSpace;
        }
    }
    
    CGSize titleSize;
    titleSize = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    
    UIEdgeInsets titleEdgeInset = UIEdgeInsetsZero;
    UIEdgeInsets imageEdgeInset = UIEdgeInsetsZero;
    CGSize imageSize = self.imageView.frame.size;
    
    if (UIButtonLayoutTypeLeftTitleRightImage == type) {
        titleEdgeInset = UIEdgeInsetsMake(0, -imageSize.width, 0, self.imageView.width + midSpace);
        imageEdgeInset = UIEdgeInsetsMake(0, titleSize.width + midSpace, 0, -titleSize.width);
    } else if (UIButtonLayoutTypeUpImageBottomTitle == type) {
        titleEdgeInset = UIEdgeInsetsMake(imageSize.height + midSpace, -imageSize.width, 0, 0);
        imageEdgeInset = UIEdgeInsetsMake(-(titleSize.height + midSpace), 0, 0, -titleSize.width);
    }
    
    [self setTitleEdgeInsets:titleEdgeInset];
    [self setImageEdgeInsets:imageEdgeInset];
}

+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font bgColor:(UIColor *)bgColor {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    [btn setBackgroundColor:bgColor];
    return btn;
}

+ (instancetype)btnWithTitle:(NSString *)title font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

+ (instancetype)btnWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font {
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    return btn;
}

- (void)setTitle:(NSString *)title font:(UIFont *)font {
    [self setTitle:title forState:UIControlStateNormal];
    [self.titleLabel setFont:font];
}

static const void *kUIButtonBlockKey = &kUIButtonBlockKey;

- (void)addTouchUpInsideHandler:(kTouchUpInsideHandlerBlock)handlerBlock {
    objc_setAssociatedObject(self, kUIButtonBlockKey, handlerBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(hookTouchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)hookTouchUpInsideAction:(UIButton *)sender {
    kTouchUpInsideHandlerBlock block = objc_getAssociatedObject(self, kUIButtonBlockKey);
    if (block) { block(sender.tag); }
}

@end