128 lines
5.9 KiB
Mathematica
128 lines
5.9 KiB
Mathematica
|
|
//
|
|||
|
|
// UIButton+QX.m
|
|||
|
|
// QXLive
|
|||
|
|
//
|
|||
|
|
// Created by 启星 on 2025/4/28.
|
|||
|
|
//
|
|||
|
|
|
|||
|
|
#import "UIButton+QX.h"
|
|||
|
|
|
|||
|
|
@implementation UIButton (QX)
|
|||
|
|
- (void)qx_layoutButtonWithEdgeInsetsStyle:(QXButtonEdgeInsetsStyle)style
|
|||
|
|
imageTitleSpace:(CGFloat)space{
|
|||
|
|
[self sizeToFit];
|
|||
|
|
[self qx_layoutButtonNOSizeToFitWithEdgeInsetsStyle:style imageTitleSpace:space];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
- (void)qx_layoutButtonNOSizeToFitWithEdgeInsetsStyle:(QXButtonEdgeInsetsStyle)style
|
|||
|
|
imageTitleSpace:(CGFloat)space {
|
|||
|
|
/**
|
|||
|
|
* 前置知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
|
|||
|
|
* 如果只有title,那它的上下左右都是相对于button的,image也是一样;
|
|||
|
|
* 如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
CGFloat spacef = ceilf(space);
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 1.得到imageView和titleLabel的宽、高
|
|||
|
|
CGFloat imageWidth = self.imageView.frame.size.width;
|
|||
|
|
CGFloat imageHeight = self.imageView.frame.size.height;
|
|||
|
|
|
|||
|
|
CGFloat labelWidth = 0.0;
|
|||
|
|
CGFloat labelHeight = 0.0;
|
|||
|
|
if (@available(iOS 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;
|
|||
|
|
UIEdgeInsets contentEdgeInsets = UIEdgeInsetsZero;
|
|||
|
|
|
|||
|
|
CGFloat imageOffSetX = labelWidth / 2.0;
|
|||
|
|
CGFloat labelOffSetX = imageWidth / 2.0;
|
|||
|
|
|
|||
|
|
CGFloat maxWidth = MAX(imageWidth,labelWidth); // 上下排布宽度肯定变小 获取最大宽度的那个
|
|||
|
|
CGFloat changeWidth = imageWidth + labelWidth - maxWidth; // 横向缩小的总宽度
|
|||
|
|
CGFloat maxHeight = MAX(imageHeight,labelHeight); // 获取最大高度那个 (就是水平默认排布的时候的原始高度)
|
|||
|
|
|
|||
|
|
// 3.根据style和spacef得到imageEdgeInsets和labelEdgeInsets的值
|
|||
|
|
switch (style) {
|
|||
|
|
case QXButtonEdgeInsetsStyleTop:
|
|||
|
|
{
|
|||
|
|
CGFloat gap = (maxHeight - MIN(imageHeight, labelHeight))/2.0;
|
|||
|
|
if (imageHeight >= labelHeight) {
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(0, imageOffSetX, 0, -imageOffSetX);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(labelHeight + gap + spacef, -labelOffSetX, -(labelHeight + gap + spacef), labelOffSetX);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(0, - changeWidth / 2.0, spacef + labelHeight, -changeWidth / 2.0);
|
|||
|
|
}else{
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(-(gap + imageHeight + spacef), imageOffSetX, gap + imageHeight + spacef, -imageOffSetX);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(0, -labelOffSetX, 0, labelOffSetX);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(spacef + imageHeight, - changeWidth / 2.0, 0, -changeWidth / 2.0);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case QXButtonEdgeInsetsStyleLeft:
|
|||
|
|
{
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(0, -spacef/2.0, 0, spacef/2.0);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(0, spacef/2.0, 0, -spacef/2.0);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(0, spacef/2.0, 0, spacef/2.0);
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case QXButtonEdgeInsetsStyleBottom:
|
|||
|
|
{
|
|||
|
|
CGFloat gap = (maxHeight - MIN(imageHeight, labelHeight))/2.0;
|
|||
|
|
if (imageHeight >= labelHeight) {
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(0,
|
|||
|
|
imageOffSetX,
|
|||
|
|
0,
|
|||
|
|
-imageOffSetX);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(-(labelHeight + gap + spacef),
|
|||
|
|
-labelOffSetX,
|
|||
|
|
labelHeight + gap + spacef,
|
|||
|
|
labelOffSetX);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(spacef + labelHeight,
|
|||
|
|
- changeWidth / 2.0,
|
|||
|
|
0,
|
|||
|
|
-changeWidth / 2.0);
|
|||
|
|
}else{
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(gap + imageHeight + spacef,
|
|||
|
|
imageOffSetX,
|
|||
|
|
-(gap + imageHeight + spacef),
|
|||
|
|
-imageOffSetX);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(0,
|
|||
|
|
-labelOffSetX,
|
|||
|
|
0,
|
|||
|
|
labelOffSetX);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(0,
|
|||
|
|
- changeWidth / 2.0,
|
|||
|
|
spacef + imageHeight,
|
|||
|
|
-changeWidth / 2.0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
case QXButtonEdgeInsetsStyleRight:
|
|||
|
|
{
|
|||
|
|
imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+spacef/2.0, 0, -labelWidth-spacef/2.0);
|
|||
|
|
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-spacef/2.0, 0, imageWidth+spacef/2.0);
|
|||
|
|
contentEdgeInsets = UIEdgeInsetsMake(0, spacef/2.0, 0, spacef/2.0);
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4.赋值
|
|||
|
|
self.titleEdgeInsets = labelEdgeInsets;
|
|||
|
|
self.imageEdgeInsets = imageEdgeInsets;
|
|||
|
|
self.contentEdgeInsets = contentEdgeInsets;
|
|||
|
|
}
|
|||
|
|
@end
|