经查阅资料及尝试,最终解决了在图片和文字垂直排列的情况下,如果文字长度变化会导致图片位置变动的问题,最开始采用了网上比较多的做法,做法如下:

@interface UIButton (UIButtonExt)
- (void)centerImageAndTitle:(float)space;
- (void)centerImageAndTitle;
@end @implementation UIButton (UIButtonExt)
- (void)centerImageAndTitle:(float)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size; // get the height they will take up as a unit
CGFloat totalHeight = (imageSize.height + titleSize.height + spacing); // raise the image and push it right to center it
self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width); // lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (totalHeight - titleSize.height),0.0);
} - (void)centerImageAndTitle
{
const int DEFAULT_SPACING = 6.0f;
[self centerImageAndTitle:DEFAULT_SPACING];
}
@end

后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现,特将成果记录一下,以便后续查阅

-(void)layoutSubviews {
[super layoutSubviews]; // Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width/;
center.y = self.imageView.frame.size.height/;
self.imageView.center = center; //Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = ;
newFrame.origin.y = self.imageView.frame.size.height + ;
newFrame.size.width = self.frame.size.width; self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = UITextAlignmentCenter;
}

通过重写,重新设置imageView的Center和titleLabel的Frame解决上述问题。

iOS - UIButton设置图片文字上图下文排列的更多相关文章

  1. iOS UIButton 设置图片文字垂直排列

    后面经过测试,如果button的文字长度变更,会导致图片位置变化,经过多次修改UIEdgeInsets的值也没有达到期望效果,最终采用集成UIButton类,重写layoutSubviews函数实现, ...

  2. UIButton 设置图片文字垂直居中排列

    #pragma mark 按钮图片文字垂直居中排列 -(void)setButtonContentCenter:(UIButton *)button { CGSize imgViewSize,titl ...

  3. iOS UIButton 设置图片平铺

    UIImage *image2 = [UIImage imageNamed:imgName]; CGFloat top = ; // 顶端盖高度 CGFloat bottom = ; // 底端盖高度 ...

  4. iOS UIButton 设置图片不变型setImage

    [btn.imageView setContentMode:UIViewContentModeScaleAspectFill];

  5. UIButton的titleEdgeInsets属性和imageEdgeInsets属性实现图片文字按要求排列

    button可以设置 titleEdgeInsets属性和 imageEdgeInsets属性来调整其image和label相对位置,具体参考http://stackoverflow.com/ques ...

  6. ios UIButton设置单选效果,以及同时设置图片和标题

    一,设置单选效果 - (void)selectedBtnPress:(UIButton*)sender { //首先把原来按钮的选中效果消除 for (int i=0;i<num;i++) {/ ...

  7. iOS按钮设置图片在上文字在下

    UIButton同时设置Title和Image后,默认是图片在左文字在右,如下图1,很多情况下我们希望图片在上图片在下,如下图2,只需要简单的几行代码,即可实现. (1)因为需要处理多个按钮,所以将实 ...

  8. iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

  9. 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 {     [ ...

随机推荐

  1. PHP Socket实现websocket(三)Stream函数

    除了socket函数也可以使用stream函数来实现服务器与客户端. 参考PHP 的Stream实现服务器客户端模型: http://php.net/manual/en/book.stream.php ...

  2. eclipse svn

    1.上传初始项目在百度云上面创建一个空版本,然后右击项目->team->shareProject在svn的url后面加上你的版本号(否则上传失败) 2.把服务器上的项目更新到自己的项目wi ...

  3. Configuration

    package edu.fzu.ir.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  4. java复制文件

    package com.test.tes; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...

  5. treeiso

    主要包括了一些树同构的代码和一些树图生成器... download

  6. ASP.NET 上的 Async/Await 简介

    原文链接 大多数有关 async/await 的在线资源假定您正在开发客户端应用程序,但在服务器上有 async 的位置吗?可以非常肯定地回答“有”.本文是对 ASP.NET 上异步请求的概念性概述, ...

  7. JustMock Lite (Free Mocking Framework For .net)

    通过 Nuget 安装 2.   官网下载(官网不行点这里) 3.   帮助文档 商业版和免费版区别概览 MockingContainer 测试类准备:一般来说也是业务类 public class C ...

  8. LIGHTSWITCH 连接 MYSQL,中文字符不能保存----解决方法。

    使用:dotConnect for MySQL () 作为 数据库连接的PROVIDER ,  在 LIGHTSWITCH 中 引用外部的MYSQL 数据源. http://www.devart.co ...

  9. 如何恢复低版本的FlashPlayer

    本人做页游开发时,游戏用户那边经常会遇到一些很奇怪的问题.比如: 1.用户进入游戏,只显示游戏部分界面,chrome浏览器是正常的,就IE死活不行. 2.进入游戏时白屏或者一直加载不上. 3.玩游戏时 ...

  10. C++中Cstring、wstring 和string互相转换总结

    通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...