//
// UIButton+ZXSImageTitleSpacing.h
//
//
// Created by CoderZXS on 2016/4/12.
// Copyright © 2016年 CoderZXS. All rights reserved.
//
#import <UIKit/UIKit.h>
// 定义一个枚举(包含了四种类型的button)
typedef NS_ENUM(NSUInteger, ZXSButtonEdgeInsetsStyle) {
ZXSButtonEdgeInsetsStyleTop, // image在上,label在下
ZXSButtonEdgeInsetsStyleLeft, // image在左,label在右
ZXSButtonEdgeInsetsStyleBottom, // image在下,label在上
ZXSButtonEdgeInsetsStyleRight // image在右,label在左
};
@interface UIButton (ZXSImageTitleSpacing)
/**
* 设置button的titleLabel和imageView的布局样式,及间距
* 使用本方法之前需要先设置好按钮的frame和image、title
* @param style titleLabel和imageView的布局样式
* @param space titleLabel和imageView的间距
*/
- (void)zxs_layoutButtonWithEdgeInsetsStyle:(ZXSButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space;
@end
*********************************************
//
// UIButton+ZXSImageTitleSpacing.m
//
//
// Created by CoderZXS on 2016/4/12.
// Copyright © 2016年 CoderZXS. All rights reserved.
//
/**纯代码创建按钮
- 第一种通过分类的方式设置按钮非常方便,只需要一行代码就足够了,不需要我们自己计算UIEngeInsetsMake,适用于纯代码创建的按钮。 如果是Xib创建的按钮就用不了。
- 第二种通过继承的方式重写layoutSubviews的方式设置按钮好处是既适用于纯代码创建的按钮,也适用于Xib创建的按钮,但是这种方法有一定的局限性,它只适用于同一类型的按钮。一类比如我一个界面中有几种不同类型的按钮,这时候就需要我们创建不同的继承UIButton 的按钮类,在layoutSubviews设置不同的位置关系。这样就相对复杂了。
*/
#import "UIButton+ZXSImageTitleSpacing.h"
@implementation UIButton (ZXSImageTitleSpacing)
- (void)zxs_layoutButtonWithEdgeInsetsStyle:(ZXSButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space {
/**
知识点:titleEdgeInsets是title相对于其上下左右的inset,跟tableView的contentInset是类似的,
如果只有title,那它上下左右都是相对于button的,image也是一样;
如果同时有image和label,那这时候image的上左下是相对于button,右边是相对于label的;title的上右下是相对于button,左边是相对于image的。
*/
// 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;
/**
MKButtonEdgeInsetsStyleTop, // image在上,label在下
MKButtonEdgeInsetsStyleLeft, // image在左,label在右
MKButtonEdgeInsetsStyleBottom, // image在下,label在上
MKButtonEdgeInsetsStyleRight // image在右,label在左
*/
// 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
switch (style) {
case ZXSButtonEdgeInsetsStyleTop: {
imageEdgeInsets = UIEdgeInsetsMake((-labelHeight - space / 2.0), 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, (-imageHeight - space / 2.0), 0);
}
break;
case ZXSButtonEdgeInsetsStyleLeft: {
imageEdgeInsets = UIEdgeInsetsMake(0, (-space / 2.0), 0, (space / 2.0));
labelEdgeInsets = UIEdgeInsetsMake(0, (space / 2.0), 0, (-space / 2.0));
}
break;
case ZXSButtonEdgeInsetsStyleBottom: {
imageEdgeInsets = UIEdgeInsetsMake(0, 0, (-labelHeight - space / 2.0), -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake((-imageHeight - space / 2.0), -imageWith, 0, 0);
}
break;
case ZXSButtonEdgeInsetsStyleRight: {
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;
}
@end
UIButton+ZXSImageTitleSpacing
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 有几个实际业务场景需要控制UIButton响应事件的时间间隔。比如: 1、当通过点击按钮来执行网络请求时,若请求耗...
- 按钮在 selected 为 YES 时,再次点击或长按,按钮的文字变成 normal 状态的了。 代码: 效果:...
- 废话不多说代码见真章!!! 使用时 导入头文件 #import "UIButton+MutableTitle.h"
- 最近做项目,会用到许多的类似美团首页button的效果,之前做法都是自定义view,view上添加button以及...