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 { [ ...
随机推荐
- VS2010链接TFS
VS2010链接TFS源代码管理器 1.打开VS2010开发工具. 2.菜单视图===>>团队资源管理器 3.点击链接到团队项目 4.点击服务器 5.点击添加 6.输入TFS服务配置信息 ...
- Level shifting a +/- 2.5V signal to 0 - 5V
Google : Op-Amp Level Shifter Level shifting a +/- 2.5V signal to 0 - 5V I have a front end module t ...
- ACPI I/O resource conflict with SMBus
ACPI I/O resource conflict with SMBus 以電子郵件傳送這篇文章BlogThis!分享至 Twitter分享至 Facebook分享到 Pinterest 這幾天遇到 ...
- mac电脑如何不生成.DS_STORE文件
执行这个命令,然后重启 defaults write com.apple.desktopservices DSDontWriteNetworkStores true
- ios取证
摘录自:<IOS取证实战> Andrew Hoog著 viaForensics公司,今年已经募集到1600万创业基金 2007-2011年初,AT&T是在美国唯一为iphone提供 ...
- 一个类有两个方法,其中一个是同步的,另一个是非同步的; 现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法?
一个类有两个方法,其中一个是同步的,另一个是非同步的:现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法? 答案:可以 验证 package com.my.te ...
- mac上parallel与virtualbox无法共存
Mac reboots when you attempt to launch Parallels Desktop 8 and Virtual Box simultaneously Article ID ...
- c语言二叉树
Department of Computing and Information SystemsCOMP10002 Foundations of AlgorithmsSemester 2, 2014As ...
- 1.什么是泛型和C#中泛型在Class上的实现
阅读目录 一:什么是泛型? 二:C#中泛型在Class上的实现 一:什么是泛型? 我们在编程的时候需要一个数据类型,但是在刚开始的时候还不确定这个数据类型是怎么样的,或者说对于不同的多个数据类型有 ...
- 彻底解决Android SDK Manager更新慢的问题
Android SDK 下载速度慢,解决方法大概有两种.第一,FQ.这种方法比较彻底,但是要想有稳定的效果还的要花大价钱.第二,有些高人直接给了SDK中各软件的下载地址,直接下载速度非常快,下载后将包 ...