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的安装和使用(通用方 ...
随机推荐
- xUnit入门一
看了下Nhibernate的入门Demo,感觉测试驱动开发会更效率.当然,你可能觉得不是还要额外编程单元测试代码吗?开发怎么会更效率? 一句话解释之,磨刀不误砍柴工. 那就开始入门吧 ~.~ 笔者使用 ...
- C#开发微信门户及应用(20)-微信企业号的菜单管理
前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...
- Java01
1.JAVA历史概述 百度百科:http://baike.baidu.com/view/29.htm 詹姆斯.高斯林 (高司令)----java之父 Sun Micros ...
- CALayer的m34 - 三维透视效果
CATransform3D transform = CATransform3DIdentity; // 修改transform的m34达到透视效果 // - 1.0 / (500 ~ 1000 效果最 ...
- React Native知识11-Props(属性)与State(状态)
一:Props(属性) 大多数组件在创建时就可以使用各种参数来进行定制.用于定制的这些参数就称为props(属性).props是在父组件中指定,而且一经指定,在被指定的组件的生命周期中则不再改变 通过 ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- 带调试器(Debugger)的ILSpy(2.2.0.1738)
2015-03-13 09:40更新: 感谢@dark89757园友提出的调试时不能查看变量的问题. 源码已修改,并提交到了github. 请查看最新发布,二进制文件和源码都在这里: 调试时可查看变量 ...
- HTML---用记事本写html
<DOCTYPE HTML> <--DOCTYPE 文档类型,浏览器按照该类型解析--> <html> <head> <title>这个是h ...
- Node.js 教程 04 - 模块系统
前言: Node.js的模块系统类似于C/C++的文件引用,可以声明对象,也可以定义类 创建对象. 大家这么理解,就简单了. 定义: 为了让Node.js的文件可以相互调用,Node.js提供了一个简 ...
- Webform(五)——内置对象(Response、Request)和Repeater中的数据增删改
一.内置对象 (一)Response对象 1.简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用R ...