123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- //
- // UIButton+RQExtension.m
- // XinShouJiaDao
- //
- // Created by 张嵘 on 2021/7/7.
- // Copyright © 2021 JCZ. All rights reserved.
- //
- #import "UIButton+RQExtension.h"
- #import <objc/runtime.h>
- static const void *UIButtonBlockKey = &UIButtonBlockKey;
- @implementation UIButton (RQExtension)
- - (void)setFixWidthScreenFont:(float)fixWidthScreenFont {
- if (fixWidthScreenFont > 0 ) {
- self.titleLabel.font = [UIFont systemFontOfSize:RQ_WIDTH(fixWidthScreenFont)];
- } else {
- self.titleLabel.font = self.titleLabel.font;
- }
- }
- - (float)fixWidthScreenFont {
- return self.fixWidthScreenFont;
- }
- - (void)setRq_titleStr:(NSString *)rq_titleStr {
- [self setTitleNormal:rq_titleStr];
- }
- - (NSString *)rq_titleStr {
- return self.titleLabel.text;
- }
- - (void)addActionHandler:(TouchedBlock)touchHandler {
- objc_setAssociatedObject(self, UIButtonBlockKey, touchHandler, OBJC_ASSOCIATION_COPY_NONATOMIC);
- [self addTarget:self action:@selector(actionTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)actionTouched:(UIButton *)btn {
- TouchedBlock block = objc_getAssociatedObject(self, UIButtonBlockKey);
- if (block) {
- block(btn.tag);
- }
- }
- - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
- [self setImage:image withTitle:title Font:10 forState:stateType];
- }
- - (void) setImage:(UIImage *)image withTitle:(NSString *)title Font:(CGFloat)font forState:(UIControlState)stateType{
- [self.titleLabel setContentMode:UIViewContentModeCenter];
- [self.titleLabel setBackgroundColor:[UIColor clearColor]];
- [self setTitleColor:RQ_MAIN_TEXT_COLOR_1 forState:stateType];
- // [self setTitleColor:kTitleColor forState:stateType];
- [self setTitle:title forState:stateType];
- [self.titleLabel setFont:RQRegularFont(font)];
-
- [self setImage:image forState:stateType];
- [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
- }
- - (void)setTitle:(NSString*)title textColor:(UIColor*)color Font:(CGFloat)font fotState:(UIControlState)stateType {
- [self setTitle:title forState:stateType];
- [self setTitleColor:color forState:stateType];
- [self.titleLabel setFont:RQRegularFont(font)];
- }
- - (void)setImage:(UIImage *)image withTitle:(NSString*)title textColor:(UIColor*)color Font:(CGFloat)font fotState:(UIControlState)stateType {
- [self.titleLabel setContentMode:UIViewContentModeCenter];
- [self.titleLabel setBackgroundColor:[UIColor clearColor]];
- [self setTitleColor:color forState:stateType];
- [self setTitle:title forState:stateType];
- [self.titleLabel setFont:RQRegularFont(font)];
- [self setImage:image forState:stateType];
- [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
- }
- -(void)setTitleNormal:(NSString*)title
- {
- [self setTitle:title forState:UIControlStateNormal];
- }
- -(void)setTitleSelect:(NSString*)title
- {
- [self setTitle:title forState:UIControlStateSelected];
- }
- -(void)target:(id)obj
- {
- [self addTarget:obj action:NSSelectorFromString(@"btnClick:") forControlEvents:UIControlEventTouchUpInside];
- }
- -(void)target:(id)obj tag:(NSInteger)tag
- {
- self.tag = tag;
- [self target:obj];
- }
- - (void)layoutButtonWithEdgeInsetsStyle:(RQButtonEdgeInsetsStyle)style
- imageTitleSpace:(CGFloat)space {
- /**
- * 知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
- * 如果只有title,那它上下左右都是相对于button的,image也是一样;
- * 如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
- */
-
- // 1. 得到imageView和titleLabel的宽、高
- CGFloat imageWith = self.imageView.image.size.width;
- CGFloat imageHeight = self.imageView.image.size.height;
-
- CGFloat labelWidth = 0.0;
- CGFloat labelHeight = 0.0;
- if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
- // 由于iOS8中titleLabel的size为0,用下面的这种设置
- labelWidth = self.titleLabel.intrinsicContentSize.width;
- labelHeight = self.titleLabel.intrinsicContentSize.height;
- } else {
- labelWidth = self.titleLabel.frame.size.width;
- labelHeight = self.titleLabel.frame.size.height;
- }
-
- // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
- UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
- UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
-
- // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
- switch (style) {
- case RQButtonEdgeInsetsStyleTop:
- {
- imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
- labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
- }
- break;
- case RQButtonEdgeInsetsStyleLeft:
- {
- imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
- labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
- }
- break;
- case RQButtonEdgeInsetsStyleBottom:
- {
- imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
- labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
- }
- break;
- case RQButtonEdgeInsetsStyleRight:
- {
- imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
- labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
- }
- break;
- default:
- break;
- }
-
- // 4. 赋值
- self.titleEdgeInsets = labelEdgeInsets;
- self.imageEdgeInsets = imageEdgeInsets;
- }
- @end
|