UIButton 图片位置调整,只需一行代码!

之前调整UIButton 的图片位置,都习惯性的用UIEdgeInsetsMake,也没什么问题,前两天改了一个文字长度,发现怎么调试都不管用,始终没法很好的居中,只好重新用自定义了一下,今天闲来无事,决定写一个居中的扩展。代码如下:

.h
#import 

typedef NS_ENUM(NSInteger, UIButtonImagePosition) {
    UIButtonImagePositionTop = 1000,
    UIButtonImagePositionRight = 1001,
    UIButtonImagePositionBottom = 1002
};

@interface UIButton (TTAdd)

///image的位置,如果设置了titleEdgeInsets 或者 imageEdgeInsets这个属性就不管用了
@property (nonatomic, assign) UIButtonImagePosition buttonImagePosition;
///image和title之间的距离,如果设置了titleEdgeInsets 或者 imageEdgeInsets 这个属性就不管用了
@property (nonatomic, assign) CGFloat buttonImageMargin;


@end
.m

#import "UIButton+TTAdd.h"
#import 



@implementation UIButton (TTAdd)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL oriSel = @selector(layoutSubviews);
        SEL newSel = @selector(tt_layoutSubviews);
        Class class = [self class];
        Method oriMethod = class_getInstanceMethod(class, oriSel);
        Method newMethod = class_getInstanceMethod(class, newSel);
        
        BOOL success = class_addMethod(class, oriSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
        if (success) {
            class_replaceMethod(class, newSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
        } else {
            method_exchangeImplementations(oriMethod, newMethod);
        }
    });
}

- (CGFloat)buttonImageMargin {
    return [objc_getAssociatedObject(self, _cmd) doubleValue];
}

- (void)setButtonImageMargin:(CGFloat)buttonImageMargin {
    [self setNeedsLayout];
    objc_setAssociatedObject(self, @selector(buttonImageMargin), @(buttonImageMargin), OBJC_ASSOCIATION_ASSIGN);
}

- (UIButtonImagePosition)buttonImagePosition {
    return [objc_getAssociatedObject(self , _cmd) integerValue];
}

- (void)setButtonImagePosition:(UIButtonImagePosition)position {
    [self setNeedsLayout];
    objc_setAssociatedObject(self, @selector(buttonImagePosition), @(position), OBJC_ASSOCIATION_ASSIGN);
}

- (void)tt_layoutSubviews {
    [self tt_layoutSubviews];
    
    BOOL hasSetTitleEdges = (self.titleEdgeInsets.left != 0 || self.titleEdgeInsets.top != 0 || self.titleEdgeInsets.bottom != 0 || self.titleEdgeInsets.right != 0 );
    BOOL hasSetImageEdges = (self.imageEdgeInsets.top != 0 || self.imageEdgeInsets.left != 0 || self.imageEdgeInsets.bottom != 0 || self.imageEdgeInsets.right != 0);
    if (hasSetTitleEdges || hasSetImageEdges) {
        return;
    }
    
    CGFloat height = self.bounds.size.height;
    CGFloat imageWidth = self.imageView.bounds.size.width;
    CGFloat imageHeight = self.imageView.bounds.size.height;
    CGFloat titleHeight = self.titleLabel.bounds.size.height;
    
    if (self.buttonImagePosition == UIButtonImagePositionTop) {
        CGRect imageNewFrame = CGRectMake(0, 0, imageWidth, imageHeight);
        imageNewFrame.origin.x = CGRectGetMidX(self.bounds) - imageWidth / 2;
        imageNewFrame.origin.y = (height - imageHeight - titleHeight - self.buttonImageMargin) / 2;
        self.imageView.frame = imageNewFrame;
        
        CGRect newFrame = [self titleLabel].frame;
        newFrame.origin.x = 0;
        newFrame.origin.y = self.imageView.frame.size.height + (height - imageHeight - titleHeight) / 2 + self.buttonImageMargin/2;
        newFrame.size.width = self.frame.size.width;
        self.titleLabel.frame = newFrame;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    } else if (self.buttonImagePosition == UIButtonImagePositionBottom) {
        CGRect titleNewFrame = CGRectMake(0, 0, self.bounds.size.width, titleHeight);
        titleNewFrame.origin.x = 0;
        titleNewFrame.origin.y = (height - imageHeight - titleHeight - self.buttonImageMargin) / 2;
        self.titleLabel.frame = titleNewFrame;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        CGRect imageNewFrame = CGRectMake(0, 0, imageWidth, imageHeight);
        imageNewFrame.origin.x = CGRectGetMidX(self.bounds) - imageWidth / 2;
        imageNewFrame.origin.y = (height - imageHeight - titleHeight) / 2 + titleHeight + self.buttonImageMargin / 2;
        self.imageView.frame = imageNewFrame;
    } else if (self.buttonImagePosition == UIButtonImagePositionRight) {
        CGRect imageFrame = self.imageView.frame;
        CGRect titleFrame = self.titleLabel.frame;
        
        CGFloat titleX = (self.frame.size.width - (imageFrame.size.width + titleFrame.size.width + self.buttonImageMargin)) / 2;
        
        self.titleLabel.frame = CGRectMake(titleX, titleFrame.origin.y, titleFrame.size.width, titleFrame.size.height);
        self.imageView.frame = CGRectMake(CGRectGetMaxX(self.titleLabel.frame) + self.buttonImageMargin, imageFrame.origin.y, imageFrame.size.width, imageFrame.size.height);
    } else {
        
        CGRect imageFrame = self.imageView.frame;
        CGRect titleFrame = self.titleLabel.frame;
        
        CGFloat imageNewX = (self.frame.size.width - (imageFrame.size.width + titleFrame.size.width + self.buttonImageMargin)) / 2;
        
        CGRect newImageFrame = CGRectMake(imageNewX, imageFrame.origin.y, imageFrame.size.width, imageFrame.size.height);
        CGRect newTitleFrame = CGRectMake(CGRectGetMaxX(newImageFrame) + self.buttonImageMargin, titleFrame.origin.y, titleFrame.size.width, titleFrame.size.height);
        self.titleLabel.frame = newTitleFrame;
        self.imageView.frame = newImageFrame;
    }
}

@end

通过重写layoutSubViews 方法来调整imageViewtitleLabel 的位置,达到最终效果。通过runTime 来交换一下方法,不用每次去继承自定义的Button

使用时仅需一句代码
self.button2.tt_buttonImagePosition = UIButtonImagePositionTop;

效果如下:

UIButton 图片位置调整,只需一行代码!_第1张图片
效果图

你可能感兴趣的:(UIButton 图片位置调整,只需一行代码!)