美文网首页
iOS-UIButton设置图片文字位置

iOS-UIButton设置图片文字位置

作者: 香蕉你个菠萝 | 来源:发表于2019-10-30 08:46 被阅读0次
当我们想设置一个Button的图片和文字的位置,是不是首想想到利用titleEdgeInsets和imageEdgeInsets属性来设置。
问题来了,设置button的这两个属性时,与图片的真实像素有关。如果多个button的话,最好保证图片像素是一样的。如果多个Button的图片给的尺寸都不一样,往往在设置的时候不好控制居中或者对其,别慌,跟着我做问题不大:

写个UIButton的分类,方便日后调用

.h


#import <UIKit/UIKit.h>

@interface UIButton (RefreshLocation)

/**
 *  图片在上边
 *  文字在下边
 */
- (void)refreshTopBottom;
/**
 *  图片在下边
 *  文字在上边
 */
- (void)refreshBottomTop;
/**
 *  图片在右边
 *  文字在左边
 */
- (void)refreshRightLeft;
/**
 *  已经用refresh刷新过不满意,可以调用此方法在原来基础上再次增加
 *
 *  @param top    上边
 *  @param bottom 下边
 *  @param left   左边
 *  @param right  右边
 */
- (void)refreshImageViewWithTop:(CGFloat)top andBottom:(CGFloat)bottom andLeft:(CGFloat)left andRight:(CGFloat)right;

- (void)refreshTitleLabelWithTop:(CGFloat)top andBottom:(CGFloat)bottom andLeft:(CGFloat)left andRight:(CGFloat)right;

//竖直排列 设置图片和文字的距离
- (void)verticalImageAndTitle:(CGFloat)spacing;
@end

.m


#import "UIButton+RefreshLocation.h"

@implementation UIButton (RefreshLocation)

- (void)refreshTopBottom{
    CGFloat btnH=self.frame.size.height;
    CGFloat btnW=self.frame.size.width;
    
    CGFloat ivX=self.imageView.frame.origin.x;
    CGFloat ivY=self.imageView.frame.origin.y;
    CGFloat ivW=self.imageView.frame.size.width;
    
    CGFloat titX=self.titleLabel.frame.origin.x;
    CGFloat titY=self.titleLabel.frame.origin.y;
    CGFloat titW=self.titleLabel.frame.size.width;
    CGFloat titH=self.titleLabel.frame.size.height;
    
    //top
    CGFloat t1=-ivY;
    CGFloat l1=btnW*0.5-(ivX+ivW*0.5);
    CGFloat b1=-t1;
    CGFloat r1=-l1;
    self.imageEdgeInsets=UIEdgeInsetsMake(t1,l1,b1,r1);
    
    CGFloat t2=btnH-titY-titH;
    CGFloat l2=btnW*0.5-(titX+titW*0.5);
    CGFloat b2=-t2;
    CGFloat r2=-l2;
    
    self.titleEdgeInsets=UIEdgeInsetsMake(t2,l2,b2,r2);
}

- (void)refreshRightLeft{
    CGFloat ivW=self.imageView.frame.size.width;
    CGFloat titW=self.titleLabel.frame.size.width;
    
    CGFloat t1=0;
    CGFloat l1=titW;
    CGFloat b1=-t1;
    CGFloat r1=-l1;
    self.imageEdgeInsets=UIEdgeInsetsMake(t1,l1,b1,r1);
    
    CGFloat t2=0;
    CGFloat l2=-ivW;
    CGFloat b2=-t2;
    CGFloat r2=-l2;
    self.titleEdgeInsets=UIEdgeInsetsMake(t2,l2,b2,r2);
}

- (void)refreshBottomTop{
    
}

- (void)refreshImageViewWithTop:(CGFloat)top andBottom:(CGFloat)bottom andLeft:(CGFloat)left andRight:(CGFloat)right{
    UIEdgeInsets edg=self.imageEdgeInsets;
    edg.top+=top;
    edg.bottom+=bottom;
    edg.left+=left;
    edg.right+=right;
    self.imageEdgeInsets=edg;
}

- (void)refreshTitleLabelWithTop:(CGFloat)top andBottom:(CGFloat)bottom andLeft:(CGFloat)left andRight:(CGFloat)right{
    UIEdgeInsets edg=self.titleEdgeInsets;
    edg.top+=top;
    edg.bottom+=bottom;
    edg.left+=left;
    edg.right+=right;
    self.titleEdgeInsets=edg;
}

- (void)verticalImageAndTitle:(CGFloat)spacing
{
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);

}

@end

记得先给Frame后调用

相关文章

网友评论

      本文标题:iOS-UIButton设置图片文字位置

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