zoukankan      html  css  js  c++  java
  • iOS按钮设置图片在上文字在下

    UIButton同时设置Title和Image后,默认是图片在左文字在右,如下图1,很多情况下我们希望图片在上图片在下,如下图2,只需要简单的几行代码,即可实现。

    [_praiseButton setImage:[UIImage imageNamed:@"Share_SendPraise"] forState:UIControlStateNormal];

    图1

    (1)因为需要处理多个按钮,所以将实现代码封装为一个方法,把每个UIbutton实例作为参数传入即可,代码如下:

    [objc] view plain copy
    1. -(void)initButton:(UIButton*)btn{  
    2.     btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示  
    3.     [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变  
    4.     [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度,其它不边  
    5. }  


    (2)然后把按钮传入

    [objc] view plain copy
      1. [self initButton:self.btn1];  
      2. [self initButton:self.btn2];  
      3. [self initButton:self.btn3];  
      4. [self initButton:self.btn4];  
      5. [self initButton:self.btn5];  
      6. [self initButton:self.btn6];  
      7. [self initButton:self.btn7];  
  • 相关阅读:
    wince5.0 key
    CSS基础知识思维导图xmind
    切图
    头部标签集
    List和Set的区别
    PicGo + Gitee 实现 Markdown 图床
    GitHub Pages 与 Gitee Pages 上的 Jekyll
    Agile, CI/CD,DevOps
    你还不了解DevOps? 看这篇就够了
    DevOps实践心得
  • 原文地址:https://www.cnblogs.com/mafeng/p/5856821.html
Copyright © 2011-2022 走看看