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;
}
文/mokong(简书作者)
原文链接:http://www.jianshu.com/p/3052a3b14a4e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

iOS 设置 文字和 图片的位置的更多相关文章

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

    代码地址如下:http://www.demodashi.com/demo/11606.html 前记 在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的 ...

  2. wps如何设置文字环绕图片

    wps在编辑一些文字的时候,经常会插入一些图片,但是插入图片后,文字和图片就被分离开来,整体显得没有那么美观整洁,这个时候就用到了软件的文字环绕功能,那么具体如何设置呢,接下来看教程. 首先打开wps ...

  3. iOS设置文字过长时的显示格式

    以label为例: //设置文字过长时的显示格式 aLabel.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间 aLabel.lineB ...

  4. iOS设置截图背景图片透明

    /** 设置图片背景为透明 */- (UIImage *)imageToTransparent { // 分配内存 const int imageWidth = self.size.width; co ...

  5. 改变Button文字和图片的位置

    button.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);button.titleEdgeInsets = UI ...

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

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

  7. IOS设置button 图片 文字 上下、左右

    [btn setImage:imgNor forState:UIControlStateNormal]; [btn setImage:imgSel forState:UIControlStateSel ...

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

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

  9. IOS 绘制基本图形(画文字、图片水印)

    - (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...

随机推荐

  1. Oracle调整联机日志大小

    近期一个项目反馈月底高峰期时系统整体性能下降,抓取对应的AWR和ASH查看,等待事件排第一位的竟然是redo日志切换.进一步看每秒的日志量是5M多,而日志文件大小仅有200M.建议项目上调整日志大小, ...

  2. Docker实践(3)—浅析device mapper的thin provision

    thin provision是在 kernel3.2 中引入的.它主要有以下一些特点: (1)允许多个虚拟设备存储在相同的数据卷中,从而达到共享数据,节省空间的目的: (2)支持任意深度的快照.之前的 ...

  3. MyBatis学习总结(七)——Mybatis缓存

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  4. python学习-day20、装饰器【图片缺失可看】印象笔记博客备份

    前言: 装饰器用于装饰某些函数或者方法,或者类.可以在函数执行之前或者执行之后,执行一些自定义的操作. 1.定义:装饰器就是一个函数,为新定义的函数.把原函数嵌套到新函数里面.以后就可以在执行新函数的 ...

  5. ruby 查询mysql方法

    首先对需要使用的数据库进行封装,便于使用:数据库表封装源码: mysqlapi.rb #业务涉及的数据库的配置ActiveRecord::Base$db1={:adapter => " ...

  6. [FFmpeg] ffmpeg参数详解

    ffmpeg 参数语法 ffmpeg [[options][`-i' input_file]]... {[options] output_file}... 如果没有输入文件,那么视音频捕捉就会起作用. ...

  7. 技术那么多,你想看看JSON Schema的测试吗?

    目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Jav ...

  8. Chap6: question 46 - 48

    46. 求 1+2+ … +n. 要求:不用乘除法.for.while.if.else.switch.case 以及条件判断语句(A?B:C). a. 利用构造函数求解 #include <io ...

  9. 7. Reverse Words in a String

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

  10. windows 装 Crypto.Cipher

    python 2.7 先去官网下载,https://www.dlitz.net/software/pycrypto/ 回来装这个,http://download.csdn.net/download/d ...