iOS-UIButton-设置button标题和图片位置
一.效果图
1.Button被点击之前

2.Button被点击之后

二.代码
- (void)createBtn3
{
UIImage * buttonImage = [UIImage imageNamed:@"1.jpg"];
UIImage * selectedImage = [UIImage imageNamed:@"2.jpg"];
CGFloat buttonImageViewWidth = CGImageGetWidth(buttonImage.CGImage);
CGFloat buttonImageViewHeight = CGImageGetHeight(buttonImage.CGImage); NSString * buttonTitle = @"左图右标题";
UIFont * buttonTitleFont = [UIFont systemFontOfSize:17.0f];
NSDictionary * attributes = @{NSFontAttributeName : buttonTitleFont};
CGSize buttonTitleLabelSize = [buttonTitle sizeWithAttributes:attributes]; // button宽度,至少为imageView宽度与titleLabel宽度之和
CGFloat buttonWith = buttonImageViewWidth + buttonTitleLabelSize.width;
// button高度,至少为imageView高度与titleLabel高度之和
CGFloat buttonHeight = buttonImageViewHeight + buttonTitleLabelSize.height; UIButton * btn3 = [[UIButton alloc] init];
[btn3 setCenter:CGPointMake(, )];
[btn3 setBounds:CGRectMake(, , buttonWith, buttonHeight)];
[btn3 setBackgroundColor:[UIColor redColor]]; [btn3.titleLabel setFont:buttonTitleFont];
[btn3 setImage:buttonImage forState:UIControlStateNormal]; [btn3 setImage:selectedImage forState:UIControlStateSelected]; [btn3 setTitle:buttonTitle forState:UIControlStateNormal]; [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn3.titleLabel setBackgroundColor:[UIColor whiteColor]]; [btn3 addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3]; }
- (void)clicked:(UIButton *)btn3
{
NSLog(@"btn1被点击了");
NSString * buttonSelectedTitle = @"上图下标题";
[btn3 setTitle:buttonSelectedTitle forState:UIControlStateNormal]; CGPoint buttonBoundsCenter = CGPointMake(CGRectGetMidX(btn3.bounds), CGRectGetMidY(btn3.bounds)); // 找出imageView最终的center
CGPoint endImageViewCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetMidY(btn3.imageView.bounds)); // 找出titleLabel最终的center
CGPoint endTitleLabelCenter = CGPointMake(buttonBoundsCenter.x, CGRectGetHeight(btn3.bounds)-CGRectGetMidY(btn3.titleLabel.bounds)); // 取得imageView最初的center
CGPoint startImageViewCenter = btn3.imageView.center; // 取得titleLabel最初的center
CGPoint startTitleLabelCenter = btn3.titleLabel.center; // 设置imageEdgeInsets
CGFloat imageEdgeInsetsTop = endImageViewCenter.y - startImageViewCenter.y; CGFloat imageEdgeInsetsLeft = endImageViewCenter.x - startImageViewCenter.x; CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop; CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft; btn3.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; btn3.titleEdgeInsets = UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
iOS-UIButton-设置button标题和图片位置的更多相关文章
- iOS - UIButton设置文字标题下划线以及下划线颜色
创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...
- iOS Button 上文字图片位置的设置
1. 添加图片+文字/文字+图片 ,不分前后,图片默认在文字前边 加空格隔开 UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake ...
- ios UIButton设置单选效果,以及同时设置图片和标题
一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...
- iOS - UIButton设置图片文字上图下文排列
经查阅资料及尝试,最终解决了在图片和文字垂直排列的情况下,如果文字长度变化会导致图片位置变动的问题,最开始采用了网上比较多的做法,做法如下: @interface UIButton (UIButton ...
- iOS UIButton 设置图片文字垂直排列
后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现, ...
- iOS UIButton 设置图片平铺
UIImage *image2 = [UIImage imageNamed:imgName]; CGFloat top = ; // 顶端盖高度 CGFloat bottom = ; // 底端盖高度 ...
- iOS 代码方式设置按钮标题、图片的偏移
button.imageEdgeInsets = UIEdgeInsetsMake(0,1 , 2, 3); button.titleEdgeInsets = UIEdgeInsetsMake(0,1 ...
- iOS UIButton 设置图片不变型setImage
[btn.imageView setContentMode:UIViewContentModeScaleAspectFill];
- ios UIButton设置高亮状态下的背景色
一,通过按钮的事件来设置背景色 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - (void)viewDidLoad { [ ...
随机推荐
- typeid详解
在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型,即允许“ ...
- 使用Microsoft.Office.Interop.Excel.Application xlApp 生成Excel
object filePath = @"C:\" + DateTime.Now.ToShortDateString().Replace("-", "& ...
- 转:如何学习javascript
ps:每过一段时间就会发觉自己懂的越来越少,打开编辑器的时候不知道从何入手.兴许是过于急于求成,总没有系统地去对这门语言进行学习,囫囵吞枣,只想着能够解决工作需求.是时候让自己重新出发,从零开始,一页 ...
- eclipse连接远程hadoop集群开发时权限不足问题解决方案
转自:http://blog.csdn.net/shan9liang/article/details/9734693 eclipse连接远程hadoop集群开发时报错 Exception in t ...
- 【News】SpagoBI中国官方微信对外发布
爱数据,爱分析,爱达比 致力于国际优秀开源BI应用在中国的普及推广,用数据开启中小企业智慧之门: 扫描上图二维码,关注SpagoBI中国官方微信,2015年8月18日将在腾讯课堂.百度传课.网易云课堂 ...
- mvc 返回 xml
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- App 卸载记录
http://blog.csdn.net/jiangwei0910410003/article/details/36427963 总结:没有root权限的情况下,还是使用Intent发送卸载请求,同时 ...
- ASP.NET 回调技术(CallBack)
在asp.net中客户端与服务器端的交互默认都是整页面提交, 此时客户端将当前页面表单中的数据(包括一些自动生成的隐藏域)都提交到服务器端,服务器重新实例化一个当前页面类的实例响应这个请求,然后将整个 ...
- CvMat 矩阵的使用方法和简单程序
一:CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type,void* data=NULL, int step=CV_AUTO ...
- 关于DDD的 认识
tks: 第一篇: 领域驱动设计系列(1)通过现实例子显示领域驱动设计的威力 第二篇: 领域驱动设计系列(2)浅析VO.DTO.DO.PO的概念.区别和用处 第三篇: 领域驱动设计系列(3)有选择性的 ...