UIButton的文本与图片的布局
UIButton内部文本和图片的布局是我们日常代码中,不可缺少的部分,按钮默认左边图片右边文本,那要实现左边文本,右边图片,我们该怎么解决呢,上面图片,下面文本又该怎么办呢
其实很简单,今天总结下,目前主要用两种方式,一种就是重写按钮,另一种就是通过setTitleEdgeInsets和setImageEdgeInsets方法解决
下图是按钮默认情况下的图文布局

左边文本,右边图片
首先介绍重写按钮吧,新建一个按钮继承UIButton,
|
1
2
3
4
5
6
7
8
9
10
11
12
|
- (void)layoutSubviews{ [super layoutSubviews]; CGRect imageRect = self.imageView.frame; imageRect.size = CGSizeMake(30, 30); imageRect.origin.x = (self.frame.size.width - 30) ; imageRect.origin.y = (self.frame.size.height - 30)/2.0f; CGRect titleRect = self.titleLabel.frame; titleRect.origin.x = (self.frame.size.width - imageRect.size.width- titleRect.size.width); titleRect.origin.y = (self.frame.size.height - titleRect.size.height)/2.0f; self.imageView.frame = imageRect; self.titleLabel.frame = titleRect;} |
效果如下:

上面图片,下面文本
同样用重写按钮的方法
|
1
2
3
4
5
6
7
8
9
10
11
|
- (void)layoutSubviews{ [super layoutSubviews]; CGRect imageRect = self.imageView.frame; imageRect.size = CGSizeMake(30, 30); imageRect.origin.x = (self.frame.size.width - 30) * 0.5; imageRect.origin.y = self.frame.size.height * 0.5 - 40; CGRect titleRect = self.titleLabel.frame; titleRect.origin.x = (self.frame.size.width - titleRect.size.width) * 0.5; titleRect.origin.y = self.frame.size.height * 0.5 ; self.imageView.frame = imageRect; self.titleLabel.frame = titleRect;} |
效果如下:
; [btn1 setImage:[UIImage imageNamed:@"icon_shouye"] forState:UIControlStateNormal]; [btn1 setTitle:@"首页" forState:UIControlStateNormal]; [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn1.backgroundColor = [UIColor redColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(50, 50, 80, 40); [btn setImage:[UIImage imageNamed:@"icon_shouye"] forState:UIControlStateNormal]; [btn setTitle:@"首页" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor redColor]; //上左下右 btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.frame.size.width - btn.imageView.frame.origin.x - btn.imageView.frame.size.width, 0, 0); btn.titleEdgeInsets = UIEdgeInsetsMake(0, -(btn.frame.size.width - btn.imageView.frame.size.width ), 0, 0); [self.view addSubview:btn1]; [self.view addSubview:btn]; |
完全颠倒的效果

上面图片下面文本
代码如下:
|
1
2
3
4
5
6
7
8
9
10
|
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(50, 50, 80, 60); [btn setImage:[UIImage imageNamed:@"icon_shouye"] forState:UIControlStateNormal]; [btn setTitle:@"首页的事" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor redColor]; btn.imageEdgeInsets = UIEdgeInsetsMake(- (btn.frame.size.height - btn.titleLabel.frame.size.height- btn.titleLabel.frame.origin.y),(btn.frame.size.width -btn.titleLabel.frame.size.width)/2.0f -btn.imageView.frame.size.width, 0, 0); btn.titleEdgeInsets = UIEdgeInsetsMake(btn.frame.size.height-btn.imageView.frame.size.height-btn.imageView.frame.origin.y, -btn.imageView.frame.size.width, 0, 0); [self.view addSubview:btn]; |
效果图:

关于setTitleEdgeInsets和setImageEdgeInsets下面进行一些解释:
UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。
显示格式区分:
1.当button.width < image.width时,只显示被压缩后的图片,图片是按照fillXY的方式压缩。
2.当button.width > image.width,且button.width < (image.width+text.width)时,图片正常显示,文本被压缩。
3.当button.width > (image.width+text.width)时,两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。
想改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。对titleLabel和imageView设置偏移是针对他当前的位置起作用的,并不是针对距离button边框的距离的。
typedefNS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
UIControlContentHorizontalAlignmentCenter =0,//居中
UIControlContentHorizontalAlignmentLeft =1,//居左
UIControlContentHorizontalAlignmentRight =2,//居右
UIControlContentHorizontalAlignmentFill =3,//
想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。感觉设置不设置UIControlContentHorizontalAlignmentCenter居中都没有影响,这个网上也找了些相关的信息,感觉都没有说到重点,我这里也没有完全理解透彻,之前都是在设置setTitleEdgeInsets和setImageEdgeInsets这些参数时都是不停的尝试得到的结果。目前这是我理解后,代码实现最后的答案,希望可以帮到大家。
UIButton的文本与图片的布局的更多相关文章
- 5分钟 搞定UIButton的文本与图片的布局
UIButton内部文本和图片的布局是我们日常代码中,不可缺少的部分,按钮默认左边图片右边文本,那要实现左边文本,右边图片,我们该怎么解决呢,上面图片,下面文本又该怎么办呢 其实很简单,今天总结下,目 ...
- IOS-UIButton的文本与图片的布局
UIButton内部文本和图片的布局是我们日常代码中,不可缺少的部分,按钮默认左边图片右边文本,那要实现左边文本,右边图片,我们该怎么解决呢,上面图片,下面文本又该怎么办呢 其实很简单,今天总结下,目 ...
- 干货之UIButton的title和image自定义布局
当需要实现一个自定义布局图片和标题的按钮时候,不知道有多少少年直接布局了UIButton,亦或是自定义一个UIView,然后以空白UIButton.UILabel.UIImageVew作为subVie ...
- C#给PDF文档添加文本和图片页眉
页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...
- 如何使用免费PDF控件从PDF文档中提取文本和图片
如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...
- C# base64编码的文本与图片互转
/// <summary> /// base64编码的文本转为图片 /// </summary> /// <param name="txtFilePath&qu ...
- 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件
[源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...
- 利用开源框架Volley来下载文本和图片。
Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简单,功能强大. 下载连接地址:http://download.csdn.net/detail/zhangphil ...
随机推荐
- firefox 插件 URLRedirector 审核通过
firefox 插件 URLRedirector 审核通过 前段时间弄的 firefox 插件,昨天通过了审核,已经在 firefox 上可以搜索和安装. 插件用 webextension 写的,代码 ...
- gitlab改用ssh操作
1.配置ssh http://blog.csdn.net/xyzchenxiaolin/article/details/51853319http://blog.csdn.net/r8hzgemq/ar ...
- SublimeText教程
1安装插件 1.請先確認已經安裝好Package Control 未安裝的話請看:安裝第一個Sublime Text套件 2.按下CTRL + SHIFT + P並且輸入Package Control ...
- App.domain http->https
App.domain = `${location.origin}/toa-mgw/rest/gateway`.replace(/http:\/\//, 'https://');
- C#下没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
C#下没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) 原因:没有原生支持64位,而是以32位兼容方式运行 解决办法:在项目属性里设置“生成” ...
- Android 主题切换 小结
前言 我们用手机的时候经常看到 设置里面有夜间模式和白天模式来更换APP的主题,以前以为很简单,但是实际做起来还是有各种不完美,那么我们也要去了解各种解决方案来丰富我们的知识,现在我们就来看看各种优劣 ...
- git基本配置
用户信息 你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式. $ git config --global user.name "Donl ...
- 安装hive+mysql
1.源码安装mysql 以root用户首先安装libaio-0.3.104.tar.gz tar zxvf libaio-0.3.104.tar.gz cd libaio-0.3.104 make p ...
- 基于MDK的mbed工程建立
个人更喜欢mdk作为IDE来编写代码,而mbed作为一个开源项目,有大量优秀代码可以借鉴使用,今后一段时间都会主要看mbed平台的代码以及国内ebox平台代码 1 首先登陆mbed在 ...
- 如何使用iTunes与iTools导出微信聊天记录
.tocblock .tocblock .tocblock { margin-left: 2.25em; } .madoko .toc>.tocblock .tocblock { margin- ...