iOS 按钮的image和title位置设置

释放双眼,带上耳机,听听看~!

经常会碰到图片和字的位置不一样的按钮,记录在此,直接贴代码
.h 文件

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,SAImageTitleLocationType) {
    SAImageLocateLeftAndTotalCenter = 0,
    SAImageLocateRightAndTotalCenter,
    SAImageLocateLeftAndTotalLeft,
    SAImageLocateRightAndTotalLeft,
    SAImageLocateLeftAndTotalRight,
    SAImageLocateRightAndTotalRight,
    SAImageLocateTopAndTotalCenter,
    SAImageLocateBottomAndTotalCenter,
};

@interface SAInsetButton : UIButton

/** image和title布局样式, 默认图左字右 */
@property (nonatomic, assign) SAImageTitleLocationType locationType;

/** 图片与文字的间距, 默认10 */
@property (nonatomic, assign) CGFloat interval;

@end

.m 文件

#import "SAInsetButton.h"

#define kDefaultInterval 10

@implementation SAInsetButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.interval = kDefaultInterval;
        self.locationType = SAImageLocateLeftAndTotalCenter;
    }
    return self;
}

- (void)setLocationType:(SAImageTitleLocationType)locationType {
    _locationType = locationType;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat titleWidth = self.titleLabel.bounds.size.width;
    CGFloat titleheight = self.titleLabel.bounds.size.height;
    CGFloat imageWidth = self.currentImage.size.width;
    CGFloat imageHeight = self.currentImage.size.height;
    switch (self.locationType) {
        case SAImageLocateLeftAndTotalLeft:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval, 0,-_interval);
        }
            break;
        case SAImageLocateLeftAndTotalCenter:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval*0.5, 0, _interval*0.5);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, _interval*0.5, 0,-_interval*0.5);
        }
            break;
        case SAImageLocateLeftAndTotalRight:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -_interval, 0, _interval);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,0);
        }
            break;
        case SAImageLocateRightAndTotalLeft:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval, 0, -(titleWidth+_interval));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
            break;
        case SAImageLocateRightAndTotalCenter:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth+_interval*0.5, 0, -(titleWidth+_interval*0.5));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval*0.5), 0, imageWidth+_interval*0.5);
        }
            break;
        case SAImageLocateRightAndTotalRight:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,titleWidth, 0, -(titleWidth));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth+_interval), 0, imageWidth+_interval);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        }
            break;
        case SAImageLocateTopAndTotalCenter:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,0, titleheight + _interval, -(titleWidth));
            self.titleEdgeInsets = UIEdgeInsetsMake(imageHeight + _interval, -(imageWidth), 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
        case SAImageLocateBottomAndTotalCenter:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0,0, -(titleheight + _interval), -titleWidth);
            self.titleEdgeInsets = UIEdgeInsetsMake(-(imageHeight + _interval),-imageWidth, 0, 0);
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }
            break;
            
        default:
            break;
    }
}

@end

人已赞赏
iOS文章

iOS应用架构谈 组件化方案

2019-10-12 14:17:18

iOS文章

iOS 重写becomeFirstResponder来实现PlaceholderLabel左右抖动,但是只能执行一次重写方法

2019-10-12 15:01:31

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索