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 { [ ...
随机推荐
- 如何设计优秀的API(转)
到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ...
- 部分设备在微信内无法播放audio的解决方案
临时接到一个紧急的需求,一个活动页面,在某台iPhone 5S设备上无法播放音频,其它设备均正常.我接到这个任务时,也是一脸懵逼,试过在audio标签上添加controls属性来显示audio,结果发 ...
- LPC18xx LPC43xx LPC4370 Bootrom USB DFU FPB - Flash Patch and Breakpoint Unit
What is the difference between a Bootrom vs bootloader on ARM systems Bootrom Bootrom (or Boot ROM) ...
- git在分支上创建目录和文件
创建一个空目录,在其中初始化git git init 创建一个新文件,此时默认在master分支上 touch file1.txt add到staging area git add file1.txt ...
- android 平台搭建
直接到官网下载 http://developer.android.com/sdk/index.html?utm_source=weibolife
- 【micropython】用python来进行BadUSB的USB-HID测试(含无线控制)
转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 本文以TPYBoardv101开发板为例讲解了利用micropython进行BadUSB的u ...
- 使用LS2J技术在LotusScript中使用Java
我们可以使用LS2J技术在LotusScript中来调用Java类,下面举例说明: 1. 首先需要在代理的 (Options)部分,添加如下: Uselsx "*javacon& ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Python从内存中使用编译后的模块
在Windows编程的时候,有些时候,我们经常会要使用一些非常规的方法,比如说从内存中加载DLL,然后使用DLL中的函数.于是就思索在用Python的时候是否能够将几个编译好的Pyc合并成一个,然后使 ...
- WPF操作ini 文件的读写示例
/// <summary> /// IniFiles 的摘要说明. /// 示例文件路径:C:\file.ini /// [Server] //[*] 表示缓存区 /// name=loc ...