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. fmap为什么可以用function作为第二个参数

    看看fmap的类型 fmap :: Functor f => (a -> b) -> f a -> f b 很明显的,第一个参数是function,第二个参数是functor的 ...

  2. ldd命令【转】

    转自:http://www.cnblogs.com/wanghetao/p/3779611.html ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库. 参数说明: --versi ...

  3. MyBatis学习总结(一)mybatis与spring整合

    MyBatis学习总结(一)mybatis与spring整合 一.需要的jar包 1.spring相关jar包 2.Mybatis相关的jar包 3.Spring+mybatis相关jar包 4.My ...

  4. 如何让natTable表格支持自定义多个右键菜单

    在nebula中,官方默认提供了一个构造natTable的builder类,并且提供了一个debugInfo的默认右键菜单,但是当我们通过官方提供的builder去创建natTable,并且要添加多个 ...

  5. hdu 5105(数学题)

    Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. .net core 发布iis 错误

    点击iis功能,例如 点击log日志,提示xxx路径下的web.config错误 百度之后 安装NET Core Windows Server Hosting ->DotNetCore.2.0. ...

  7. 推荐一个国内的免费公共静态cdn

    https://cdn.baomitu.com/ 更新速度比bootcdn要快,收录的库也很齐全.

  8. 洛谷——P2660 zzc 种田

    P2660 zzc 种田 题目背景 可能以后 zzc就去种田了. 题目描述 田地是一个巨大的矩形,然而zzc 每次只能种一个正方形,而每种一个正方形时zzc所花的体力值是正方形的周长,种过的田不可以再 ...

  9. 洛谷——P1107 最大整数

    P1107 最大整数 题目描述 设有n个正整数 (n<=20), 将它们连接成一排, 组成一个最大的多位整数. 例如: n=3时, 3个整数13, 312, 343连接成的最大整数为: 3433 ...

  10. 消息队列集群kafka安装配置

    1. 下载wget http://mirror.rise.ph/apache/kafka/0.11.0.0/kafka_2.12-0.11.0.0.tgz2. 安装tar xf kafka_2.12- ...