iOS UIButton調(diào)整文字和圖片的位置

???? 前言:UIButton的位置默認(rèn)是左圖右文字

一、直接設(shè)置左文字右圖,利用原有屬性 semanticContentAttribute

在需要圖片和文字,而且還能點(diǎn)擊的控件,button 是當(dāng)之無愧的選擇,button 默認(rèn)的模式是圖片在左,文字在右,以往需要調(diào)整文字和圖片的順序的時(shí)候,需要重寫 button,今天發(fā)現(xiàn)了一個(gè)新的辦法,就是一個(gè)叫 semanticContentAttribute 的屬性,有四個(gè)選擇,默認(rèn)就是 Unspecified,然后選擇Force Right-to-Left 就變成圖片在右邊,文字在左邊的效果了
testButton.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

二、寫一個(gè)分類,可以實(shí)現(xiàn)多種組合

上文字下圖,上圖下文字 ,左圖右文字,左文字右圖

上文字下圖.png

上圖下文字.png

左圖右文字.png

左文字右圖.png

UIButton+zt_adjustImageAndTitle.h
//  PresentTest
//
//  Created by xzq on 2020/8/10.
//  Copyright ? 2020年 xzq. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
 使用范例:
 
     UIButton    * testButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
     testButton.backgroundColor = [UIColor redColor];
     [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     [testButton setTitle:@"我是測試" forState:UIControlStateNormal];
     [testButton setImage:[UIImage imageNamed:@"home_icon_arrow"] forState:UIControlStateNormal];
 
     testButton.zt_contentAdjustType = ZTContentAdjustImageDownTitleUp;
     testButton.zt_space = 5;
     [testButton zt_beginAdjustContent];
 
     [self.view addSubview:testButton];
 */

typedef NS_ENUM(NSUInteger, ZTContentAdjustType) {
    ZTContentAdjustImageLeftTitleRight = 0,// default
    ZTContentAdjustImageRightTitleLeft,
    ZTContentAdjustImageUpTitleDown,
    ZTContentAdjustImageDownTitleUp
};

@interface UIButton (zt_adjustImageAndTitle)
@property (nonatomic, assign) ZTContentAdjustType    zt_contentAdjustType;//圖片與文字的結(jié)構(gòu) 默認(rèn)圖片在左,文字在右
@property (nonatomic, assign) CGFloat    zt_space;// 圖片與文字的間距 默認(rèn)是5
/*
 開始調(diào)整內(nèi)容
 調(diào)用前,請確保設(shè)置好title以及image
 */
- (void)zt_beginAdjustContent;
/*
 zt_beginAdjustContent 默認(rèn)在下一次runloop進(jìn)行更新,這個(gè)方法提供直接更新
 */
- (void)zt_beginAdjustContentImmediately;
/*
 可以傳入文字最大寬度
 */
- (void)zt_beginAdjustContentWithMaxTitleWidth:(CGFloat)maxTitleWidth;
@end
UIButton+zt_adjustImageAndTitle.m
//
//  UIButton+zt_adjustImageAndTitle.m
//  PresentTest
//
//  Created by xzq on 2020/8/10.
//  Copyright ? 2020年 xzq. All rights reserved.
//

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

const void * zt_spaceKey = "zt_spaceKey";
const void * zt_contentAdjustTypeKey = "zt_contentAdjustTypeKey";

@implementation UIButton (zt_adjustImageAndTitle)

@dynamic zt_contentAdjustType,zt_space;


- (void)zt_beginAdjustContent {
    [self zt_beginAdjustContentWithMaxTitleWidth:0];
}

- (void)zt_beginAdjustContentImmediately {
    [self _zt_beginAdjustContentWithMaxTitleWidth:0];
}

- (void)zt_beginAdjustContentWithMaxTitleWidth:(CGFloat)maxTitleWidth {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self _zt_beginAdjustContentWithMaxTitleWidth:maxTitleWidth];
    });
}

- (void)zt_beginAdjustContentImmediatelyWithMaxTitleWidth:(CGFloat)maxTitleWidth {
    [self _zt_beginAdjustContentWithMaxTitleWidth:maxTitleWidth];
}

#pragma mark---- private

- (void)_zt_beginAdjustContentWithMaxTitleWidth:(CGFloat)maxTitleWidth {
    UIImage    * btnImage = self.imageView.image;
    NSString    * btnTitle = self.titleLabel.text;
    
    if (!btnImage || btnTitle.length <= 0) {
        NSAssert(false, @"請先設(shè)置按鈕的圖片以及文字");
        return;
    }
    
    CGSize imageSize = btnImage.size;
    CGFloat imageWidth = imageSize.width;
    CGFloat imageHeight = imageSize.height;
    
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeZero];
    CGFloat titleWidth = titleSize.width;
    CGFloat titleHeight = titleSize.height;
    
    if (maxTitleWidth > 0 && titleWidth > maxTitleWidth) {
        titleWidth = maxTitleWidth;
    }
    
    CGFloat space = self.zt_space;
    
    switch (self.zt_contentAdjustType) {
        case ZTContentAdjustImageLeftTitleRight: {
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, (space*0.5), 0, -(space*0.5))];
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, -(space*0.5), 0, (space*0.5))];
        }
            break;
        case ZTContentAdjustImageRightTitleLeft: {
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imageWidth+space*0.5), 0, (imageWidth+space*0.5))];
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, (titleWidth + space*0.5), 0, -(titleWidth + space*0.5))];
        }
            break;
        case ZTContentAdjustImageUpTitleDown: {
            [self setTitleEdgeInsets:UIEdgeInsetsMake((titleHeight+space)*0.5, -imageWidth*0.5, -(titleHeight+space)*0.5, imageWidth*0.5)];
            [self setImageEdgeInsets:UIEdgeInsetsMake(-(imageHeight+space)*0.5, titleWidth*0.5, (imageHeight+space)*0.5, -titleWidth*0.5)];
        }
            break;
        case ZTContentAdjustImageDownTitleUp: {
            [self setTitleEdgeInsets:UIEdgeInsetsMake(-(titleHeight+space)*0.5, -imageWidth*0.5, (titleHeight+space)*0.5, imageWidth*0.5)];
            [self setImageEdgeInsets:UIEdgeInsetsMake((imageHeight+space)*0.5, titleWidth*0.5, -(imageHeight+space)*0.5, -titleWidth*0.5)];
        }
            break;
        default: {
            [self setTitleEdgeInsets:UIEdgeInsetsMake(0, (space*0.5), 0, -(space*0.5))];
            [self setImageEdgeInsets:UIEdgeInsetsMake(0, -(space*0.5), 0, (space*0.5))];
        }
            break;
    }
}

#pragma mark---- getter and setter

- (CGFloat)zt_space {
    NSNumber * objc = objc_getAssociatedObject(self, zt_spaceKey);
    if (!objc) {
        return 5;
    }
    return [objc floatValue];
}

- (void)setZt_space:(CGFloat)zt_space {
    objc_setAssociatedObject(self, zt_spaceKey, @(zt_space), OBJC_ASSOCIATION_RETAIN);
}

- (ZTContentAdjustType)zt_contentAdjustType {
    NSNumber * objc = objc_getAssociatedObject(self, zt_contentAdjustTypeKey);
    return [objc floatValue];
}

- (void)setZt_contentAdjustType:(ZTContentAdjustType)zt_contentAdjustType {
    objc_setAssociatedObject(self, zt_contentAdjustTypeKey, @(zt_contentAdjustType), OBJC_ASSOCIATION_RETAIN);
}

@end



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容