UIButton的imageView和titleLabel的位置设置通过setImageEdgeInsets和setTitleEdgeInsets来设置

参考:http://blog.csdn.net/dfqin/article/details/37813591http://blog.sina.com.cn/s/blog_5df876f301016h8h.html

实现如上图的效果其实有多种方法,像在button上嵌套label,imageView即可,下面是通过调节button自带的titleLabel和imageView来实现。

自定义一个创建button的方法,传入titleLabel的text和图片名称,图片不应该太大,width和height超过button的长宽时图片会被压缩

- (UIButton *)creatBtnWithTitle:(NSString *)title andImageName:(NSString *)image
{
UIImage *buttonImage = [UIImage imageNamed:image];
CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage)
;
CGFloat buttonImageViewHeight = CGImageGetHeight(buttonImage.CGImage); NSString *buttonTitle = title; UIFont *buttonTitleFont = [UIFont systemFontOfSize:18.0f];
  
CGSize buttonTitleLabelSize = [buttonTitle sizeWithAttributes:@{NSFontAttributeName:buttonTitleFont}];
// button宽度,至少为imageView宽度与titleLabel宽度之和 CGFloat buttonWidth = buttonImageViewWidth + buttonTitleLabelSize.width; // button高度,至少为imageView高度与titleLabel高度之和 CGFloat buttonHeight = buttonImageViewHeight + buttonTitleLabelSize.height; UIButton *tempBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; [tempBtn setBounds:CGRectMake(, , buttonWidth, buttonHeight)]; [tempBtn.titleLabel setFont:buttonTitleFont]; [tempBtn setImage:buttonImage forState:UIControlStateNormal]; [tempBtn.imageView setBackgroundColor:[UIColor clearColor]]; [tempBtn setTitle:buttonTitle forState:UIControlStateNormal]; [tempBtn.titleLabel setBackgroundColor:[UIColor clearColor]]; CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(tempBtn.bounds), CGRectGetMidY(tempBtn.bounds)); // 找出imageView最终的center CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x + tempBtn.bounds.size.width/-tempBtn.imageView.bounds.size.width/, buttonBoundsCenter.y); // 找出titleLabel最终的center CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x-tempBtn.bounds.size.width/ + tempBtn.titleLabel.bounds.size.width/, buttonBoundsCenter.y); // 取得imageView最初的center CGPoint startImageViewCenter = tempBtn.imageView.center; // 取得titleLabel最初的center CGPoint startTitleLabelCenter = tempBtn.titleLabel.center; // 设置imageEdgeInsets CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y; CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x; CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop; CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft; tempBtn.imageEdgeInsets = UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight); // 设置titleEdgeInsets CGFloat titleEdgeInsetsTop = endTitleLabelCenter.y-startTitleLabelCenter.y; CGFloat titleEdgeInsetsLeft = endTitleLabelCenter.x - startTitleLabelCenter.x; CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop; CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft; tempBtn.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight); return tempBtn;
}

可以再实现方法中创建出一个button

UIButton *btn = [self creatBtnWithTitle:@"爱妃,请叫我朕" andImageName:@"chat_bottom_up_nor"];

    btn.layer.cornerRadius = ;

    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

    [btn setTitleColor:[UIColor groupTableViewBackgroundColor] forState:(UIControlStateHighlighted)];

    btn.layer.borderWidth = 0.5;

    btn.frame = CGRectMake(, , , );

    [self.view addSubview:btn];

iOS学习-UIButton的imageView和titleLabel的更多相关文章

  1. iOS 对UIButton的imageView和titleLabel进行重新布局

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. ios开发之--UIButton中imageView和titleLabel的位置调整

    在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...

  3. 格而知之1:UIButton中imageView和titleLabel的位置调整

    在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...

  4. iOS 中UIButton的 settitle 和 titlelabel的使用误区

    UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void) ...

  5. IOS学习--UIButton常用方法(20150122)

    // 1.创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 2.添加按钮 [self.view a ...

  6. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

  7. (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等

    UIButton是一个常用控件,使用方法十分基本,但是有很多技巧常常不被注意,本文主要介绍UIButton的一些较高级技巧,用于实现图片和标签显示的美观性等. 开发时常常碰到按钮的下侧或者右侧有标题的 ...

  8. ios学习-delegate、传值、跳转页面

    ios学习-delegate.传值.跳转页面     1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...

  9. 【原】iOS学习之第三方-AFNetworking1.3.0

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 AFNetworking 第三方集成到工程中,具体请看上篇博客iOS学习46之第三方CocoaPods的安装和使用(通用方 ...

随机推荐

  1. C# 本质论 第四章 方法和参数

    要为方法名使用动词或动词短语 递归:递归调用方法 方法重载: try catch

  2. .net 刷新页面防止表单二次提交

    1.页面上按钮是服务器控件,现在刷新页面要防止按钮事件重复执行 原网址:http://blog.csdn.net/high_mount/article/details/51066056

  3. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(六)

    (六)在Website Cloud中添加site 1新建Website,并打开 使用前面创建的用户 newbee@waplab.com 登录租户Portal,新建一个website 新建完成后, 可以 ...

  4. Delphi_01_控制台版HelloWorld

    对于Windows下的控制台编程,我相信很多人都不陌生.而C语言开始的著名的“Hello world”程序基本是学习编程的第一步.我想对于 RAD开发,大家熟悉的一般都是GUI编程,而对于consol ...

  5. 理解RESTful架构

    越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

  6. GJM : Unity3D HIAR -【 快速入门 】 七、使用本地识别包

    使用本地识别包 本文将向您介绍如何在 Unity 工程中使用本地识别包. Step 1.下载本地识别包 前往 HiAR 管理后台,上传您需要识别的图片并下载识别包,您可以获得一个 unitypacka ...

  7. JDBC数据库访问操作的动态监测 之 p6spy

    P6spy是一个JDBC Driver的包装工具,p6spy通过对JDBC Driver的封装以达到对SQL语句的监听和分析,以达到各种目的. P6spy1.3 sf.net http://sourc ...

  8. Android 网络框架之Retrofit2使用详解及从源码中解析原理

    就目前来说Retrofit2使用的已相当的广泛,那么我们先来了解下两个问题: 1 . 什么是Retrofit? Retrofit是针对于Android/Java的.基于okHttp的.一种轻量级且安全 ...

  9. Spring 4 使用Freemarker模板发送邮件&添加附件

    前言 Spring对Java的邮件发送提供了很好的支持,提供了超级简单的API,大大简化了Java邮件发送功能的开发. Spring对Email的支持是基于JavaMail API开发的,所以,我们在 ...

  10. SQL Server 2012 新特性:新增和修改函数

    转换函数      1.PARSE      Parse是把字符串类型转化为想要的类型,看看和convert和cast的区别         SELECT PARSE ('2.111111' AS f ...