创建图文混排并整体居中的UIButton

之前一直对UIButton的图文混排一直很迷糊,知道可以通过titleEdgeInset和imageEdgeInset属性来调整,但一直不知道inset的各个值是相对谁的或者说怎么计算的。直到前天看见2篇关于图文混排的文章参考文章1参考文章2,才豁然开朗,然后自己写代码实践并记录下(它们的比我考虑的情况多,本篇假设button的高宽都足够容纳下文本和图片的情况)。友情提示:如果不想看计算过程可以直接跳到尾部copy代码
如后一篇文章里说的的前置知识点:
imageEdgeInsets(top,left,bottom,right) 和 titleEdgeInsets(top,left,bottom,right)的值就是我们想要的最终结果与按钮初始状态比较图片和标题的位置分别在上下左右位移的值
对于各种排版就知道怎么计算titleEdgeInset和imageEdgeInset,下面配合图文说明就很容易理解了。
当设置好button的图片和文本后的初始状态如下:

初始状态

这个时候是图片和文本组成的整体是居中于button里的,且图片在左,文本在右。项目里经常遇到图片和文本之间有个padding啊,或者图片在文本上边,右边或者下边的情况。如下图:
图片和文本之间只是多了个padding:

这个时候怎么计算titleEdgeInset和imageEdgeInset呢?就是用最终状态的坐标减去初始状态的坐标。具体来说,对于imageView,它的垂直坐标没变,也就是top和bottom都是0,titleLabel也是。imageView的左边相对于而言往左移动了padding / 2.0,相应的titleLabel 相比原来而言则右移了padding/2.0。算下来就是:

imageEdgeInset =  UIEdgeInsetsMake(0, -padding / 2.0, 0, padding / 2.0);
titleEdgeInset = UIEdgeInsetsMake(0, padding / 2.0, 0, -padding / 2.0);

对于图片在右,文本在左的排版如下图:

同样的,imageView和titleLabel的垂直左边均没有改变,top和bottom都是0,imageView的左边相对于其初始状态向右移动了titleLabel.size.width+padding / 2.0,其左边自然也相对于其初始状态左边右移了titleLabel.size.width+padding / 2.0,titleLabel的左边相对于初始状态左边左移了imageView.size.wdith + padding / 2.0,其右边自然也相对于其初始状态右边左移了imageView.size.wdith + padding / 2.0,计算下来:

imageEdgeInset =  UIEdgeInsetsMake(0, titleLabel.frame.size.width + padding / 2.0, 0, -titleLabel.frame.size.width - padding / 2.0);
titleEdgeInset = UIEdgeInsetsMake(0, -imageView.frame.size.width - padding / 2.0, 0, imageView.frame.size.width + padding / 2.0);

对于图片在上,文本在下的排版如下:

图片在左

imageView和titleLabel作为一个整体是居中于button里的,因此可以偏移量可以如下计算:

imageEdgeInset.left = button.frame.size.width/ 2.0 -  CGRectGetMidX(imageView.frame)//通过他们的水平中心点来计算
titleEdgeInset.left =  button.frame.size.width/ 2.0 -  CGRectGetMidX(titleLabel.frame)//通过他们的水平中心点来计算

对于top的计算同样根据最终的坐标减去初始坐标,先计算最终态的imageView.origin.y

y  = (button.frame.size.height - imageView.frame.size.height - padding - titleLabel.frame.size.height) / 2.0;

因此:

imageEdgeInset.top =  y - imageView.frame.origin.y = (button.frame.size.height - imageView.frame.size.height - padding - titleLabel.frame.size.height) / 2.0 - imageView.frame.origin.y;

同理可以算出titlLabel的top偏移量为

titleEdgeInset.top = y + imageView.frame.size.height + padding - titleLabel.frame.origin.y = (button.frame.size.height - imageView.frame.size.height - padding - titleLabel.frame.size.height) / 2.0 + imageView.frame.size.height + padding - titleLabel.frame.origin.y = button.frame.size.height / 2.0 + imageView.frame.size.height / 2.0 + padding / 2.0 - titleLabel.frame.origin.y;

是时候综合下了:

imageEdgeInset = UIEdgeInsetsMake(y - imageView.frame.origin.y, button.frame.size.width/ 2.0 -  CGRectGetMidX(imageView.frame),  -y + imageView.frame.origin.y, -button.frame.size.width/ 2.0 +  CGRectGetMidX(imageView.frame));
titleEdgeInset = UIEdgeInsetsMake(y + imageView.frame.size.height + padding - titleLabel.frame.origin.y, button.frame.size.width/ 2.0 -  CGRectGetMidX(titleLabel.frame), -(y + imageView.frame.size.height + padding - titleLabel.frame.origin.y), -button.frame.size.width/ 2.0 +  CGRectGetMidX(titleLabel.frame));

代码复制好累,最后一种情况图片在下,文本在上:
计算思路,同上一种情况一样,不写具体的计算代码了最后总结下自己写了一个UIButton的分类,有兴趣的可以直接拷贝下面这段

typedef NS_ENUM(NSInteger, CPButtonStyle) {
    CPButtonStyleDefault = 0,
    CPButtonStyleImageUp,
    CPButtonStyleImageRight,
    CPButtonStyleImageBottom
};

- (void)setButtonStyle:(CPButtonStyle)style imageTitlePadding:(CGFloat)padding {

    CGRect tempFrame = self.frame;
    UILabel *label = [[UILabel alloc] init]; //为了防止拿不到titleLabel的高宽,新建一个UILabel,获取高宽
    label.font = self.titleLabel.font;
    label.text = self.titleLabel.text;
    [label sizeToFit];
    CGRect titleFrame = label.frame;
    
    UIImageView *imageView = [[UIImageView alloc] init];//为了防止拿不到button的imageView的高宽,新建一个UIImageView,获取高宽
    imageView.image = self.imageView.image;
    [imageView sizeToFit];
    CGRect imageFrame = imageView.frame;
    if (imageFrame.size.width == 0 || imageFrame.size.height == 0 || titleFrame.size.width == 0 || titleFrame.size.height == 0) {
        padding = 0;
    }
    CGRect frame = self.frame;
    frame.size.width = SCREEN_WIDTH; //这样做目的是尽可能显示下图片和标题
    frame.size.height = SCREEN_HEIGHT;
    CGFloat iTop, iLeft, iRight, iBottom;
    CGFloat tTop, tLeft, tRight, tBottom;
    iTop = iLeft = iRight = iBottom = tTop = tLeft = tRight = tBottom = 0.0;
    if (style == CPButtonStyleImageRight) {
        iLeft = titleFrame.size.width + padding / 2.0;
        tLeft = -imageFrame.size.width - padding / 2.0;
    }
    if (style == CPButtonStyleImageUp) {
        CGFloat temp = (frame.size.height - (titleFrame.size.height + imageFrame.size.height + padding)) / 2.0;
        iLeft = frame.size.width / 2.0 - CGRectGetMidX(imageFrame);
        iTop = temp - imageFrame.origin.y;
        
        tLeft = frame.size.width / 2.0 - CGRectGetMidX(titleFrame);
        tTop = temp + imageFrame.size.height + padding - titleFrame.origin.y;
    }
    
    if (style == CPButtonStyleImageBottom) {
        CGFloat temp = (frame.size.height - (titleFrame.size.height + imageFrame.size.height + padding)) / 2.0;
        iLeft = frame.size.width / 2.0 - CGRectGetMidX(imageFrame);
        iTop = temp + titleFrame.size.height + padding - imageFrame.origin.y;
        
        tLeft = frame.size.width / 2.0 - CGRectGetMidX(titleFrame);
        tTop = temp - titleFrame.origin.y;
    }
    if (style == CPButtonStyleDefault) {
        iLeft = -padding /2.0;
        tLeft = padding / 2.0;
    }
    iRight = -iLeft;
    iBottom = -iTop;
    
    tRight = -tLeft;
    tBottom = -tTop;
    self.imageEdgeInsets = UIEdgeInsetsMake(iTop, iLeft, iBottom, iRight);
    self.titleEdgeInsets = UIEdgeInsetsMake(tTop, tLeft, tBottom, tRight);
    if (style == CPButtonStyleImageRight || style == CPButtonStyleDefault) {//设置完后,button的大小为刚好容纳图片和标题的大小,原始的sizeToFit方法不会算入这个padding
        tempFrame.size.width = imageFrame.size.width + titleFrame.size.width + padding;
        tempFrame.size.height = MAX(imageFrame.size.height, titleFrame.size.height);
    } else {
        tempFrame.size.width = MAX(imageFrame.size.width, titleFrame.size.width);
        tempFrame.size.height = imageFrame.size.width + titleFrame.size.width + padding;
    }
    self.frame = tempFrame;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,073评论 25 707
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,705评论 1 92
  • iOS的UIButton是一个非常常见而且常用的控件,我们一般用他来实现某个功能的提交以及选择操作。我们可以建立只...
    欧阳大哥2013阅读 6,887评论 18 125
  • retain cycle产生情况 A和B两个对象,A持有B,B同时也持有A,A只有B释放之后才有可能释放,同样B只...
    Mystical阅读 223评论 0 0
  • 从大学和男人交往两年了,一直都很愉快。毕业后,我们都留在北京工作,有结婚打算。最近我们计画一起住,一来省房租,二来...
    高浩容阅读 31,300评论 139 414