iOS 设置 文字和 图片的位置
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的显示):
- titleEdgeInsets是titleLabel相对于其上下左右的inset,跟tableView的contentInset是类似的;
- 如果只有title,那titleLabel的 上下左右 都是 相对于Button 的;
- 如果只有image,那imageView的 上下左右 都是 相对于Button 的;
- 如果同时有image和label,那image的 上下左 是 相对于Button 的,右 是 相对于label 的;
label的 上下右 是 相对于Button的, 左 是 相对于label 的。
上面一段很重要
理解了,然后我们就可以看懂下面的代码
- 创建一个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;
- .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;
}
原文链接:http://www.jianshu.com/p/3052a3b14a4e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
iOS 设置 文字和 图片的位置的更多相关文章
- iOS UIButton文字和图片间距随意调整
代码地址如下:http://www.demodashi.com/demo/11606.html 前记 在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的 ...
- wps如何设置文字环绕图片
wps在编辑一些文字的时候,经常会插入一些图片,但是插入图片后,文字和图片就被分离开来,整体显得没有那么美观整洁,这个时候就用到了软件的文字环绕功能,那么具体如何设置呢,接下来看教程. 首先打开wps ...
- iOS设置文字过长时的显示格式
以label为例: //设置文字过长时的显示格式 aLabel.lineBreakMode = UILineBreakModeMiddleTruncation; //截去中间 aLabel.lineB ...
- iOS设置截图背景图片透明
/** 设置图片背景为透明 */- (UIImage *)imageToTransparent { // 分配内存 const int imageWidth = self.size.width; co ...
- 改变Button文字和图片的位置
button.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);button.titleEdgeInsets = UI ...
- iOS·UIButton如何文字在下图片在上
创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 -(void)layout ...
- IOS设置button 图片 文字 上下、左右
[btn setImage:imgNor forState:UIControlStateNormal]; [btn setImage:imgSel forState:UIControlStateSel ...
- 代码设置UIButton文字、图片位置
假设有按钮rButton的 imageEdgeInsets和contentEdgeInsets可以设置按钮的标题和图片的位置,如下代码,设置标题居右 NSString * rBtnTitle = @& ...
- IOS 绘制基本图形(画文字、图片水印)
- (void)drawRect:(CGRect)rect { // Drawing code // [self test]; // 1.加载图片到内存中 UIImage *image = [UIIm ...
随机推荐
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
先手工生出一个数据框吧 import numpy as np import pandas as pd df = pd.DataFrame(np.arange(0,60,2).reshape(10,3) ...
- sql查看数据字典(表结构)
SELECT (case when a.colorder=1 then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, (case when ...
- .NET获取客户端、服务器端的信息
. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:Page.Request.UserHostNa ...
- RocketMQ安装与部署说明
一.安装说明1.下载安装包,下载地址:https://github.com/alibaba/RocketMQ/releases/download/v3.1.7/alibaba-rocketmq-3.1 ...
- Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
今天遇到一个项目中用到Android 6.0的系统,但是有个文件是从Assert目录下写到SDCard中,但是AndroidMinifest.xml中已经加了文件的读权限和写权限,异常日志是在创建文件 ...
- 核心动画和UIView动画的区别
核心动画和UIView动画的区别 1.核心动画制作用在Layer 2.核心动画的修改的属性都是假象,他的真实位置没有发生变化()
- eclipse默认文件编码
eclipse里 就是在eclipse.ini文件里添加一行-Dfile.encoding=utf-8即可 -startup plugins/org.eclipse.equinox.launcher_ ...
- l类型转换错误ClassCastException
出现问题原因story中参数写错:
- (转)SQLServer_T-SQL 语句执行时间的查询
原文地址:http://blog.csdn.net/wangjunhe/article/details/7211974 --利用getdatedeclare @beginTime datetime d ...
- uboot和内核波特率不同
uboot和内核波特率不同,在uboot启动后,修改uboot参数: set bootargs 'noinitrd root=/dev/mtdblock3 init=/linuxrc console= ...