iOS学习-UIButton的imageView和titleLabel
UIButton的imageView和titleLabel的位置设置通过setImageEdgeInsets和setTitleEdgeInsets来设置
参考:http://blog.csdn.net/dfqin/article/details/37813591及http://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的更多相关文章
- iOS 对UIButton的imageView和titleLabel进行重新布局
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- ios开发之--UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- 格而知之1:UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void) ...
- IOS学习--UIButton常用方法(20150122)
// 1.创建一个自定义的按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 2.添加按钮 [self.view a ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
- (一〇九)UIButton的使用技巧 -imageView、titleLabel、圆角等
UIButton是一个常用控件,使用方法十分基本,但是有很多技巧常常不被注意,本文主要介绍UIButton的一些较高级技巧,用于实现图片和标签显示的美观性等. 开发时常常碰到按钮的下侧或者右侧有标题的 ...
- ios学习-delegate、传值、跳转页面
ios学习-delegate.传值.跳转页面 1.打开xcode,然后选择ios--Application--Empty Application一个空项目. 项目目录: 2.输入项目名称以及选 ...
- 【原】iOS学习之第三方-AFNetworking1.3.0
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 AFNetworking 第三方集成到工程中,具体请看上篇博客iOS学习46之第三方CocoaPods的安装和使用(通用方 ...
随机推荐
- js正则表达式校验非负整数:^\d+$ 或 ^[1-9]\d*|0$
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CentOS 6.2 Eclipse CDT 开发环境搭建
一.安装中文语言支撑 我当初安装CentOS6.2时选择了中文语言支持,有iBus和拼音输入法,有gnome和kde 没有安装的可以参考:CentOS英文环境下使用中文输入法 安装中文语言支持和输入 ...
- 运用css,对于下拉菜单的制作
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <met ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- MySQL5.7 error log时间显示问题
最近有两三套环境升级到了5.7.16,发现mysql.err中的时间好像有些问题,经查是mysql 5.7后的变更,如下: root@localhost [(none)]>select now( ...
- Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39
Atitit. Atiposter 发帖机 新特性 poster new feature v7 q39 V8 重构iocutilV4,use def iocFact...jettyUtil V ...
- Web 开发中很实用的10个效果【附源码下载】
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
- 几句话就能让你理解:this、闭包、原型链
以下是个人对这三个老大难的总结(最近一直在学习原生JS,翻了不少书,不少文档,虽然还是新手,但我会继续坚持走我自己的路) 原型链 所有对象都是基于Object.prototype,Object.pro ...
- WEB基础原理——理论复习
基本WEB原理 1. Internet同Web的关系 1.1互联网 全世界最大的局域网. 来源美国国防部的项目用于数据共享 没有TCP/IP之前最开始只能1000台电脑通信(军用协议) 1.2 万维网 ...
- CRM 2013 相关下载 / 2013-10-11
CRM 2013的安装文件,软件开发工具包(Sdk)以及实施指南,在微软官方网站已经有下载了. 具体地址如下: Name Url 发布日期 语言版本 说明 CRM Server htt ...