1缕探、我最開始實現(xiàn)這個采用的方法:
重新自定義一個view,然后有兩個屬性label和imageView唆铐,然后設置位置布局哲戚,再添加單擊手勢,用代理回傳點擊方法艾岂。
2顺少、第二種方法:
自定義一個Button繼承Button,有兩個屬性label和imageView王浴,然后設置布局脆炎。這樣就不用添加單擊手勢。
3氓辣、第三種方法:
直接用Button自帶的titleLabel和imageView腕窥,寫個Category,用來設置label和image的排列方式筛婉,eg:上下、左右
明顯前兩種是很不好的癞松,所以這里只說第三種:
Button有兩個屬性:titleEdgeInsets和imageEdgeInsets爽撒,通過設置這兩個,就可以實現(xiàn)所有需要的Button的樣式响蓉,如:image在上硕勿、image在下、image在左枫甲、image在右源武。
在設置這兩個之前,我們先要理解Button上面的titleLabel和imageView的位置關系(想象Button默認的image和label的顯示):
titleEdgeInsets是titleLabel相對于其上下左右的inset想幻,跟tableView的contentInset是類似的粱栖;
如果只有title,那titleLabel的 上下左右 都是 相對于Button 的脏毯;
如果只有image闹究,那imageView的 上下左右 都是 相對于Button 的;
如果同時有image和label食店,那image的 上下左 是 相對于Button 的渣淤,右 是 相對于label 的;
label的 上下右 是 相對于Button的吉嫩, 左 是 相對于label 的价认。
上面一段很重要
理解了,然后我們就可以看懂下面的代碼
創(chuàng)建一個Button的Category自娩,.h文件如下用踩,定義一個Enum,用于決定image和label的樣式;一個方法用于調(diào)用設置捶箱。
#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;
.m文件如下晨川,實現(xiàn)方法
#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. 根據(jù)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;
}