美文网首页
iOS UIButton调整文字和图片的位置

iOS UIButton调整文字和图片的位置

作者: 最强的小强 | 来源:发表于2020-12-01 14:10 被阅读0次

    👉🏻 前言:UIButton的位置默认是左图右文字

    一、直接设置左文字右图,利用原有属性 semanticContentAttribute

    在需要图片和文字,而且还能点击的控件,button 是当之无愧的选择,button 默认的模式是图片在左,文字在右,以往需要调整文字和图片的顺序的时候,需要重写 button,今天发现了一个新的办法,就是一个叫 semanticContentAttribute 的属性,有四个选择,默认就是 Unspecified,然后选择Force Right-to-Left 就变成图片在右边,文字在左边的效果了
    testButton.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;

    二、写一个分类,可以实现多种组合

    上文字下图,上图下文字 ,左图右文字,左文字右图

    上文字下图.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;//图片与文字的结构 默认图片在左,文字在右
    @property (nonatomic, assign) CGFloat    zt_space;// 图片与文字的间距 默认是5
    /*
     开始调整内容
     调用前,请确保设置好title以及image
     */
    - (void)zt_beginAdjustContent;
    /*
     zt_beginAdjustContent 默认在下一次runloop进行更新,这个方法提供直接更新
     */
    - (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, @"请先设置按钮的图片以及文字");
            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
    
    
    
    

    相关文章

      网友评论

          本文标题:iOS UIButton调整文字和图片的位置

          本文链接:https://www.haomeiwen.com/subject/txgddktx.html