今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

實現Button文字(titleLabel)和圖片(imageView)上下排列

實現 Button 文字 (titleLabel) 和圖片 (imageView) 上下排列#

1、我最開始實現這個採用的方法:
重新自定義一個 view,然後有兩個屬性 label 和 imageView,然後設置位置佈局,再添加單擊手勢,用代理回傳點擊方法。

2、第二種方法:
自定義一個 Button 繼承 Button,有兩個屬性 label 和 imageView,然後設置佈局。這樣就不用添加單擊手勢。

3、第三種方法:
直接用 Button 自帶的 titleLabel 和 imageView,寫個 Category,用來設置 label 和 image 的排列方式,eg:上下、左右

明顯前兩種是很不好的,所以這裡只說第三種:

Button 有兩個屬性:titleEdgeInsets 和 imageEdgeInsets,通過設置這兩個,就可以實現所有需要的 Button 的樣式,如:image 在上、image 在下、image 在左、image 在右。

在設置這兩個之前,我們先要理解 Button 上面的 titleLabel 和 imageView 的位置關係(想像 Button 默認的 image 和 label 的顯示):

  1. titleEdgeInsets 是 titleLabel 相對於其上下左右的 inset,跟 tableView 的 contentInset 是類似的;
  2. 如果只有 title,那 titleLabel 的 上下左右 都是 相對於 Button 的;
  3. 如果只有 image,那 imageView 的 上下左右 都是 相對於 Button 的;
  4. 如果同時有 image 和 label,那 image 的 上下左相對於 Button 的,相對於 label 的;
    label 的 上下右相對於 Button 的相對於 label 的。

上面一段很重要#

理解了,然後我們就可以看懂下面的代碼

  1. 創建一個 Button 的 Category,.h 文件如下,定義一個 Enum,用於決定 image 和 label 的樣式;一個方法用於調用設置。

#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, MKButtonEdgeInsetsStyle) {
    MKButtonEdgeInsetsStyleTop, // image在上,label在下
    MKButtonEdgeInsetsStyleLeft, // image在左,label在右
    MKButtonEdgeInsetsStyleBottom, // image在下,label在上
    MKButtonEdgeInsetsStyleRight // image在右,label在左
};
@interface UIButton (ImageTitleSpacing)
/**
 *  設置button的titleLabel和imageView的佈局樣式,及間距
 *
 *  @param style titleLabel和imageView的佈局樣式
 *  @param space titleLabel和imageView的間距
 */
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space;
  1. .m 文件如下,實現方法

#import "UIButton+ImageTitleSpacing.h"
@implementation UIButton (ImageTitleSpacing)
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的寬、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由於iOS8中titleLabel的size為0,用下面的這種設置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 聲明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根據style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case MKButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case MKButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    // 4. 賦值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

我寫了個 Demo 在 github 上,地址:自定義 Button

參考#

  1. UIButton 的 titleEdgeInsets 屬性和 imageEdgeInsets 屬性實現圖片文字按要求排列,這篇博客裡介紹了上面的原理,建議仔細看,理解之後就可以明白
  2. UIButton 的 imageEdgeInsets 和 titleEdgeInsets,這篇博客末尾有 github 地址,我寫的時候沒有理解原理,所以設置的時候,參照了他的代碼
  3. 如何佈局包含 Image 和 Title 的 UIButton,這篇是我最開始參考的,只有代碼,沒有原理
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。