UIButton圖文布局

關(guān)于實現(xiàn)UIButton的上圖下文、上文下圖、左圖右文、右圖左文的布局需求,我也嘗試過擴展分類使用EdgeInsets來實現(xiàn),但總是不太靈活不太如意。
于是我選擇了暴力解決辦法:使用UIButton的子類,重寫layoutSubviews。

JXLayoutButton
JXLayoutButton

JXLayoutButton.h

//
//  JXLayoutButton.h
//  JXLayoutButtonDemo
//
//  Created by JiongXing on 16/9/24.
//  Copyright ? 2016年 JiongXing. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, JXLayoutButtonStyle) {
    JXLayoutButtonStyleLeftImageRightTitle,
    JXLayoutButtonStyleLeftTitleRightImage,
    JXLayoutButtonStyleUpImageDownTitle,
    JXLayoutButtonStyleUpTitleDownImage
};

/// 重寫layoutSubviews的方式實現(xiàn)布局,忽略imageEdgeInsets、titleEdgeInsets和contentEdgeInsets
@interface JXLayoutButton : UIButton

/// 布局方式
@property (nonatomic, assign) JXLayoutButtonStyle layoutStyle;
/// 圖片和文字的間距,默認值8
@property (nonatomic, assign) CGFloat midSpacing;
/// 指定圖片size
@property (nonatomic, assign) CGSize imageSize;

@end

JXLayoutButton.m

//
//  JXLayoutButton.m
//  JXLayoutButtonDemo
//
//  Created by JiongXing on 16/9/24.
//  Copyright ? 2016年 JiongXing. All rights reserved.
//

#import "JXLayoutButton.h"

@implementation JXLayoutButton

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.midSpacing = 8;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (CGSizeEqualToSize(CGSizeZero, self.imageSize)) {
        [self.imageView sizeToFit];
    }
    else {
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,
                                          self.imageView.frame.origin.y,
                                          self.imageSize.width,
                                          self.imageSize.height);
    }
    [self.titleLabel sizeToFit];
    
    switch (self.layoutStyle) {
        case JXLayoutButtonStyleLeftImageRightTitle:
            [self layoutHorizontalWithLeftView:self.imageView rightView:self.titleLabel];
            break;
        case JXLayoutButtonStyleLeftTitleRightImage:
            [self layoutHorizontalWithLeftView:self.titleLabel rightView:self.imageView];
            break;
        case JXLayoutButtonStyleUpImageDownTitle:
            [self layoutVerticalWithUpView:self.imageView downView:self.titleLabel];
            break;
        case JXLayoutButtonStyleUpTitleDownImage:
            [self layoutVerticalWithUpView:self.titleLabel downView:self.imageView];
            break;
        default:
            break;
    }
}

- (void)layoutHorizontalWithLeftView:(UIView *)leftView rightView:(UIView *)rightView {
    CGRect leftViewFrame = leftView.frame;
    CGRect rightViewFrame = rightView.frame;
    
    CGFloat totalWidth = CGRectGetWidth(leftViewFrame) + self.midSpacing + CGRectGetWidth(rightViewFrame);
    
    leftViewFrame.origin.x = (CGRectGetWidth(self.frame) - totalWidth) / 2.0;
    leftViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(leftViewFrame)) / 2.0;
    leftView.frame = leftViewFrame;
    
    rightViewFrame.origin.x = CGRectGetMaxX(leftViewFrame) + self.midSpacing;
    rightViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(rightViewFrame)) / 2.0;
    rightView.frame = rightViewFrame;
}

- (void)layoutVerticalWithUpView:(UIView *)upView downView:(UIView *)downView {
    CGRect upViewFrame = upView.frame;
    CGRect downViewFrame = downView.frame;
    
    CGFloat totalHeight = CGRectGetHeight(upViewFrame) + self.midSpacing + CGRectGetHeight(downViewFrame);
    
    upViewFrame.origin.y = (CGRectGetHeight(self.frame) - totalHeight) / 2.0;
    upViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(upViewFrame)) / 2.0;
    upView.frame = upViewFrame;
    
    downViewFrame.origin.y = CGRectGetMaxY(upViewFrame) + self.midSpacing;
    downViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(downViewFrame)) / 2.0;
    downView.frame = downViewFrame;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    [super setImage:image forState:state];
    [self setNeedsLayout];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    [super setTitle:title forState:state];
    [self setNeedsLayout];
}

- (void)setMidSpacing:(CGFloat)midSpacing {
    _midSpacing = midSpacing;
    [self setNeedsLayout];
}

- (void)setImageSize:(CGSize)imageSize {
    _imageSize = imageSize;
    [self setNeedsLayout];
}

@end

測試

//
//  ViewController.m
//  JXLayoutButtonDemo
//
//  Created by JiongXing on 16/9/24.
//  Copyright ? 2016年 JiongXing. All rights reserved.
//

#import "ViewController.h"
#import "JXLayoutButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat margin = 20;
    CGFloat buttonWidth = (CGRectGetWidth(self.view.frame) - 20 * 3) / 2.0;
    CGFloat buttonHeight = buttonWidth;
    
    [self addButton: [self generateButtonWithStyle:JXLayoutButtonStyleUpImageDownTitle]
           withFrame:CGRectMake(margin, 64 + margin, buttonWidth, buttonHeight)];
    
    [self addButton: [self generateButtonWithStyle:JXLayoutButtonStyleUpTitleDownImage]
          withFrame:CGRectMake(margin + buttonWidth + margin, 64 + margin, buttonWidth, buttonHeight)];
    
    [self addButton: [self generateButtonWithStyle:JXLayoutButtonStyleLeftImageRightTitle]
          withFrame:CGRectMake(margin, 64 + margin + buttonHeight + margin, buttonWidth, buttonHeight)];
    
    [self addButton: [self generateButtonWithStyle:JXLayoutButtonStyleLeftTitleRightImage]
          withFrame:CGRectMake(margin + buttonWidth + margin, 64 + margin + buttonHeight + margin, buttonWidth, buttonHeight)];
    
    JXLayoutButton *customSizeButton = [self generateButtonWithStyle:JXLayoutButtonStyleUpImageDownTitle];
    [customSizeButton setTitle:@"SmallSize" forState:UIControlStateNormal];
    customSizeButton.imageSize = CGSizeMake(100 / 4, 140 / 4);
    [self addButton:customSizeButton
          withFrame:CGRectMake((CGRectGetWidth(self.view.frame) - buttonWidth) / 2.0, 64 + margin * 3 + buttonHeight * 2, buttonWidth, buttonHeight)];
}

- (JXLayoutButton *)generateButtonWithStyle:(JXLayoutButtonStyle)style {
    JXLayoutButton *button = [JXLayoutButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"luffy"] forState:UIControlStateNormal];
    [button setTitle:@"Luffy" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
    button.layoutStyle = style;
    
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor magentaColor].CGColor;
    return button;
}

- (void)addButton:(JXLayoutButton *)button withFrame:(CGRect)frame {
    button.frame = frame;
    [self.view addSubview:button];
}
@end

結(jié)果如上圖。
項目源碼:https://github.com/JiongXing/JXLayoutButton
下載地址:https://github.com/JiongXing/JXLayoutButton/archive/master.zip

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

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

  • 前言:在移動 APP 的設(shè)計中,我們會經(jīng)常看到同時帶有圖片和文字的按鈕,這些按鈕在 UI 設(shè)計師眼中,可能不值一提...
    ShannonChenCHN閱讀 11,793評論 5 34
  • 我一直說我是一個很慢熱的人,這樣的說法其實并不準確,應(yīng)該說,我是一個很自我的人,所以其實我很少關(guān)注別人。一直覺得做...
    宵汀閱讀 353評論 5 5
  • 世界上的人類就毅力而言,呈棗核型分布。一種是意志力強大的人,自我管控能力卓越,這種人無論天賦如何,比較容易實現(xiàn)自我...
    fb9c0d993b24閱讀 526評論 0 0
  • 感覺今晚有人跟蹤我 ...... 我摒著氣息保持鎮(zhèn)靜快步往家走, 風(fēng)越刮越大 …… 腳步越來越響 …… 我拳頭一攥...
    龍川垚閱讀 298評論 0 0
  • 航哥哥,謝謝你! 從16年到17年,我們已經(jīng)結(jié)婚了整整一年!雖然,我們依然聚少離多,但是我們依然憧憬著未來!昨天,...
    李豫一閱讀 535評論 0 2

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