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. Python subprocess.Popen communicate() 和wait()使用上的区别

    之所以会纠结到这个问题上是因为发现在调用Popen的wait方法之后程序一直没有返回.google发现wait是有可能产生死锁的.为了把这个问题彻底弄清楚,搜索一些资料过来看看: 原文链接:http: ...

  2. Java接口响应超时监控

    为什么要监控 服务化接口是提供服务的,接口正确性.稳定性是最最重要的,在保证正确的同时需要尽量提高接口响应时间. 有的团队会有专门的工具来对系统响应时间.吞吐量做监控,但如果团队没有这种"待 ...

  3. chunkupload文件上传断点续传组件(java)

    chunkupload简介 chunkupload是一款基于java语言的断点续传组件,针对文件上传,非文件下载,集成方便,使用简单. 从整体上讲,chunkupload会对文件进行切片处理,每个切片 ...

  4. Linux Cmd Tool 系列之—alias

    The alias cmd list your current aliases. For example : alias Use alias to shorten a long cmd in curr ...

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

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

  6. EL表达式的算术运算

    一个例子--乘法运算 ${book.bookCount * book.bookPrice } 两个不同对象的EL表达式的算术运算同理 ${student.studentNum * book.bookP ...

  7. 《JS实现复制内容到剪贴板功能,可兼容所有PC浏览器,不兼容手机端》

    前记:本来原生的JS是有提供一个函数来实现这个功能(window.clipboardData),但是很遗憾,这个函数仅仅支持IE和FF浏览器,所以基本用处不大.下边介绍的是一个第三方插件库(ZeroC ...

  8. 在Android中,使用Kotlin的 API请求简易方法

    原文标题:API request in Android the easy way using Kotlin 原文链接:http://antonioleiva.com/api-request-kotli ...

  9. React Native之 ScrollView介绍和使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  10. 转载:android自定义view实战(温度控制表)!

    效果图 package cn.ljuns.temperature.view; import com.example.mvp.R; import android.content.Context;impo ...