代码地址如下:
http://www.demodashi.com/demo/11606.html

前记

在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的那种,比如可能图片在上,文字在下,或者图片在左,文字在右,关键是还有一定的距离,并不是系统默认UIButton中,图片和文字的间距。当然,这调整图片和文字的距离的小事,是难不倒大家的,因为大家都知道,在UIButton中,有这么两个属性titleEdgeInsetsimageEdgeInsets

关于属性

关于titleEdgeInsetsimageEdgeInsets这两个属性,简单介绍下:

titleEdgeInsetstitle相对于其上下左右的inset,跟tableViewcontentInset是类似的,如果只有title,那它上下左右都是相对于button的,imageEdgeInsets也是一样

如果同时有imagetitle,那这时候image的上左下是相对于button,右边是相对于title的;title的上右下是相对于button,左边是相对于image的,也就是说image是默认居左的。

通过以上两个属性,可以很轻松的实现文字图片的位置及距离的调整,如果每次在用的时候,都去重新写一片,难免会很麻烦,鉴于此,之前在谋篇博客中(具体忘了,原博主看到请勿见怪),看到过用category封装的,于是我在项目中,也写了一个category,其中也列举了四种不同的枚举来表现其不同的场景

枚举类型

// 定义一个枚举(包含了四种类型的button)
typedef NS_ENUM(NSUInteger, GLButtonEdgeInsetsStyle) {
GLButtonEdgeInsetsStyleTop, // image在上,label在下
GLButtonEdgeInsetsStyleLeft, // image在左,label在右
GLButtonEdgeInsetsStyleBottom, // image在下,label在上
GLButtonEdgeInsetsStyleRight // image在右,label在左
};

并且定义了一个简单使用的方法:

/**
* 设置button的titleLabel和imageView的布局样式,及间距
*
* @param style titleLabel和imageView的布局样式
* @param space titleLabel和imageView的间距
*/
- (void)layoutButtonWithEdgeInsetsStyle:(GLButtonEdgeInsetsStyle)style
imageTitleSpace:(CGFloat)space;

其中space是指图片文字之间的间距,style对应上面的枚举,可以针对性的选择

具体实现方法

- (void)layoutButtonWithEdgeInsetsStyle:(GLButtonEdgeInsetsStyle)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.image.size.width;
CGFloat imageHeight = self.imageView.image.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 GLButtonEdgeInsetsStyleTop:
{
imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
}
break;
case GLButtonEdgeInsetsStyleLeft:
{
imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
}
break;
case GLButtonEdgeInsetsStyleBottom:
{
imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
}
break;
case GLButtonEdgeInsetsStyleRight:
{
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;
}

实现效果

其实思路很简单,这里只是简单封装了下。

项目文件截图:

iOS UIButton文字和图片间距随意调整

代码地址如下:
http://www.demodashi.com/demo/11606.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

iOS UIButton文字和图片间距随意调整的更多相关文章

  1. iOS 设置 文字和 图片的位置

    1.我最开始实现这个采用的方法:重新自定义一个view,然后有两个属性label和imageView,然后设置位置布局,再添加单击手势,用代理回传点击方法. 2.第二种方法:自定义一个Button继承 ...

  2. 代码设置UIButton文字、图片位置

    假设有按钮rButton的 imageEdgeInsets和contentEdgeInsets可以设置按钮的标题和图片的位置,如下代码,设置标题居右 NSString * rBtnTitle = @& ...

  3. UIButton的竖排图片和文本

    UIButton的竖排图片和文本html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirro ...

  4. iOS UIButton 图片文字上下垂直布局 解决方案

    实现如图所示效果: 这是一个UIButton,需要改变image和title相对位置. 解决如下: //设置文字偏移:向下偏移图片高度+向左偏移图片宽度 (偏移量是根据[图片]大小来的,这点是关键)b ...

  5. iOS·UIButton如何文字在下图片在上

    创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 -(void)layout ...

  6. UIButton 文字图片排列

    UIButton缺省值是:图画-文字水平,所以我们并不需要调整. 1.写作-图画 水平显示,以前的文本,图片后再次 [btn setTitleEdgeInsets:UIEdgeInsetsMake(0 ...

  7. ios UIButton设置单选效果,以及同时设置图片和标题

    一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...

  8. IOS下拉放大图片

    代码地址如下:http://www.demodashi.com/demo/11623.html 一.实现效果图 现在越来越多的APP中存在下拉放大图片的效果,今天贡献一下我的实现这种方法的原理,和我遇 ...

  9. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...

随机推荐

  1. C# 日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别(转)

    日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别http://www.cnblogs.com/jhxk/articles/ ...

  2. [BZOJ1853][Scoi2010]幸运数字 容斥+搜索剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 3202  Solved: 1198[Submit][Status ...

  3. 自己在用的几个sublime text3插件

    Arduino-like IDE ConvertToUTF8 Emmet(前身是zen coding) Keymap Redefiner Package Control PyV8 SideBarEnh ...

  4. 洛谷——P1586 四方定理

    P1586 四方定理 题目描述 四方定理是众所周知的:任意一个正整数nn,可以分解为不超过四个整数的平方和.例如:25=1^{2}+2^{2}+2^{2}+4^{2}25=12+22+22+42,当然 ...

  5. hihocoder1062 最近公共祖先·一

    #1062 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在 ...

  6. 【最大权闭合子图/最小割】BZOJ3438-小M的作物【待填】

    [题目大意] 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1...n编号),现在,第i种作物种植在A中种植 ...

  7. 千克与磅之间的转换 Exercise05_05

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:千克与磅之间的转换 * */ public class Exercise05_05 { public static void ...

  8. 编译boost到各个系统平台 mac,iOS,linux,android,wind

    编译boost到各个系统平台 mac,iOS,linux,android,wind git地址:https://github.com/czjone/boost git仓库:https://github ...

  9. a + b ——C语言初学者百题大战之四

    #include <stdio.h> int main() { int a,b; scanf("%d %d",&a,&b); printf(" ...

  10. flask 开发环境搭建

    window下: 1)安装python 2)安装pip 3)使用pip install flask 如果成功安装使用pip list 既可以查看到flask的版本 ubuntu下的环境搭建 同样地使用 ...