UIButton *first = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kHeight(80), kHeight(80))];
[first setImage:[UIImage imageNamed:@"icon_index_housekeeping_10"] forState:UIControlStateNormal];
[first setTitle:@"扫一扫" forState:UIControlStateNormal];
[first.titleLabel setFont:kFont(14)];
[first setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[first setTitleColor:kColor(0x444444ff) forState:UIControlStateHighlighted];
[first setBackgroundColor:[UIColor whiteColor]];
[first setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[first setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
[first setTitleEdgeInsets:UIEdgeInsetsMake((kGetHeigh(first.bounds) - first.titleLabel.intrinsicContentSize.height ) /2 + first.imageView.intrinsicContentSize.height / 2 + (((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2), (kGetWidth(first.bounds) - first.titleLabel.intrinsicContentSize.width) / 2 - first.imageView.intrinsicContentSize.width, 0,0)];
[first setImageEdgeInsets:UIEdgeInsetsMake((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height ) /2 - (((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2), (kGetWidth(first.bounds) - first.imageView.intrinsicContentSize.width)/2, 0, 0)];

注释:

(((kGetHeigh(first.bounds) - first.imageView.intrinsicContentSize.height - first.titleLabel.intrinsicContentSize.height)/2)/2)

文字向下的偏移量   (总高度-图片高度-文字高度)/2)/2

上面的算法过于复杂,重新写了一个:

 self.contentHorizontalAlignment = .left;
self.contentVerticalAlignment = .top;

这两个方法会让图片和文字按照居左居上进行排列,如果不做任何改动,会显示如下:

设置完图片和文字的顺序后,可以进行修饰了
 //设置排列顺序为左上
self.contentHorizontalAlignment = .left;
self.contentVerticalAlignment = .top;
if style == .CusButtonStyleOne {
//图上字下
let Titletop = (kGetHeigh(self.bounds) - self.imageView!.intrinsicContentSize.height - self.titleLabel!.intrinsicContentSize.height)/ * + self.imageView!.intrinsicContentSize.height; let Titleleft = (kGetWidth(self.bounds) - self.titleLabel!.intrinsicContentSize.width) / - self.imageView!.intrinsicContentSize.width;
self.titleEdgeInsets = UIEdgeInsets.init(top:Titletop , left: Titleleft, bottom: , right: ); let imageTop = (kGetHeigh(self.bounds) - self.titleLabel!.intrinsicContentSize.height - self.imageView!.intrinsicContentSize.height) / ;
let imgLeft = (kGetWidth(self.bounds) - self.imageView!.intrinsicContentSize.width)/;
self.imageEdgeInsets = UIEdgeInsets.init(top: imageTop, left: imgLeft, bottom: , right: );
NSLog("%@", self.titleLabel?.text ?? "");
}
显示如下:



 

iOS 通用button 上图下字的更多相关文章

  1. 自定义控件,上图下字的Button,图片任意指定大小

    最近处在安卓培训期,把自己的所学写成博客和大家分享一下,今天学的是这个自定义控件,上图下字的Button安卓自带,但是苦于无法设置图片大小(可以在代码修改),今天自己做了一个,首先看一下效果图,比较实 ...

  2. iOS通用链接(Universal Links)突然点击无效的解决方案

    接上文<微信中通过页面(H5)直接打开本地app的解决方案>已经把iOS搞定并且已经正常能跑了,突然就再也用不了了... 问题描述 测试告诉我,如果从微信打开App之后,点击App右上角的 ...

  3. (转)查询或修改iPhone的短信服务中心号码(iOS通用)

    有些时候会有提示你自己的iPhone无法发送短信,原因是没有设置短信中心号码,下面就介绍一下如何设置iPhone的短信中心号码和iPhone查看短信中心号码的方法.(iOS通用) iPhone的设置短 ...

  4. iOS 在 ARC 环境下 dealloc 的使用、理解误区

    iOS 在 ARC 环境下 dealloc 的使用.理解误区 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&qu ...

  5. 李洪强iOS经典面试题下

    李洪强iOS经典面试题下 21. 下面的代码输出什么? @implementation Son : Father - (id)init { self = [super init]; if (self) ...

  6. iOS 点击cell下拉

    iOS  点击cell下拉 代码如下: #import "ViewController.h" @interface ViewController ()<UITableView ...

  7. 改动导航栏上返回button上的字,比如把back改动为返回

    改动导航栏上返回button上的字,比如把back改动为返回 注意:这个须要在跳转之前到视图控制器中写,而不是在跳转之后到控制器中写 UIBarButtonItem *backIetm = [[UIB ...

  8. 【Bootstrap3.0建站笔记二】button可下拉弹出层

    1.button可下拉弹出层: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hpbmFwbGFu/font/5a6L5L2T/fontsize/400 ...

  9. [Android]Button按下后修改背景图

    Button按下后修改背景图 错误做法:为Button添加OnTouch事件监听,根据ACTION_UP和ACTION_DOWN动作来修改Button的背景图 错误原因:从理论上讲,按钮按下修改背景色 ...

随机推荐

  1. Pandas之DataFrame——Part 2

    ''' [课程2.] 时间模块:datetime datetime模块,主要掌握:datetime.date(), datetime.datetime(), datetime.timedelta() ...

  2. WCF技术剖析 Two

    WCF终结点和寻址之--AddressHead信息匹配代码 Contracts契约 using System; using System.Collections.Generic; using Syst ...

  3. 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑

    看到知乎上有个关于linux多进程.多线程的讨论:http://www.zhihu.com/question/19903801/answer/14842584 自己项目里也对这个问题有过很多探讨和测试 ...

  4. python 向mysql插入数据

    生成随机内容用到的方法: substr是一个字符串函数,从第二个参数1,开始取字符,取到3 + floor(rand() * 75)结束 floor函数代表的是去尾法取整数. rand()函数代表的是 ...

  5. python 操作数据库1--连接、执行sql语句

    #!/usr/bin/env python # -*- coding:utf-8 -*- # @Time : 2017/11/20 16:03 # @Author : lijunjiang # @Fi ...

  6. Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP【转载】

    前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...

  7. vim的最最基本配置

    全部用户生效 /etc/vimrc 当前用户生效 ~/.vimr # 1.设置语法高亮syntax on # 2.显示行号 set nu # 3.设置换行自动缩进为4个空格 # 4.设置tab缩进为空 ...

  8. git 本地与远程关联流程

    git init git add -A git commit -m '提交' git remote add origin git@github.com:laniu/liuna.git git push ...

  9. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  10. 这绝对是有史以来最详细的web前端学习路线

    定要善用开发者工具.firefox的firebug和Chrome的F12都是很好的选择,用好了这个必会发现他带给你的帮助比看一本书更多.你把firebug摸透了你还担心对DOM理解不够?考虑到未来,h ...