iOS DeveloperiOS 开发技巧

UIButton的titleEdgeInsets属性和image

2016-12-20  本文已影响135人  提笔挥墨

UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的
contentHorizontalAlignment居左居右或居中显示。(默认是居中)

当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。
 
当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。
 
当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。
 
想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。 需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。
首先,给大家推荐一个第三方,因为UI的东西其实都是比较简单的,大家工作的用用到的时候,知道有这么个东西就行了。https://github.com/search?utf8=%E2%9C%93&q=CenterImageAndTitle

导入到工程,就只有两个文件

屏幕快照 2016-12-20 下午1.31.54.png

直接上代码,看看怎么用的吧。

@interface TestMasonryViewController ()
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) UILabel *lab;
@property (nonatomic, strong) UIView *viewFunction;
@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;
@property (nonatomic, strong) UIButton *btn3;
@property (nonatomic, strong) UIButton *btn4;
@property (nonatomic, strong) UIButton *btn5;
@property (nonatomic, strong) UIButton *btn6;
@property (nonatomic, strong) UIButton *btn7;
@end

@implementation TestMasonryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
       // 样式一:图片在上,文字在下, 整体居中
    self.viewFunction = [[UIView alloc] init];
    [self.view addSubview:self.viewFunction];
    self.viewFunction.backgroundColor = HEXCOLOR(0xffffff);
    
    [self.viewFunction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(100);
        make.height.mas_equalTo(80);
    }];
    
    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn1 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn1 setTitle:@"电影票" forState:UIControlStateNormal];
    [self.btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.viewFunction addSubview:self.btn1];
    
    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn2 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn2 setTitle:@"演出票" forState:UIControlStateNormal];
    [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.viewFunction addSubview:self.btn2];

    self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn3 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn3 setTitle:@"优惠券" forState:UIControlStateNormal];
    [self.btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn3 verticalCenterImageAndTitle:10];
    [self.viewFunction addSubview:self.btn3];
    
    self.btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn4 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn4 setTitle:@"影城卡" forState:UIControlStateNormal];
    [self.btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn4 verticalCenterImageAndTitle:10];
    [self.viewFunction addSubview:self.btn4];

    [self.btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(Screen_Width / 4);
        make.height.mas_equalTo(self.viewFunction.mas_height);
    }];
    
    [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.btn1.mas_right);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(self.btn1.mas_width);
        make.height.mas_equalTo(self.viewFunction.mas_height);
    }];

    [self.btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.btn2.mas_right);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(self.btn1.mas_width);
        make.height.mas_equalTo(self.viewFunction.mas_height);
    }];
    
    [self.btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.btn3.mas_right);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(self.btn1.mas_width);
        make.height.mas_equalTo(self.viewFunction.mas_height);
    }];

    // 注意一定要在 btn 设置完frame 之后再设置。 否者会出问题
    [self.btn1 verticalCenterImageAndTitle:10];
    [self.btn2 verticalCenterImageAndTitle:10];
    [self.btn3 verticalCenterImageAndTitle:10];
    [self.btn4 verticalCenterImageAndTitle:10];
    self.btn1.backgroundColor = RandColor;
    self.btn2.backgroundColor = RandColor;
    self.btn3.backgroundColor = RandColor;
    self.btn4.backgroundColor = RandColor;
    


    // 样式2 : 图片在左,文字在右
    self.btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn5 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn5 setTitle:@"影城卡烦到死案发后爱的色放" forState:UIControlStateNormal];
    [self.btn5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:self.btn5];
    [self.btn5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(self.viewFunction.mas_bottom).offset(10);
    }];
    
    self.btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn6 setImage:[UIImage imageNamed:@"法国"] forState:UIControlStateNormal];
    [self.btn6 setTitle:@"影城卡烦" forState:UIControlStateNormal];
    [self.btn6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:self.btn6];
    [self.btn6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(self.btn5.mas_bottom).offset(10);
    }];

    [self.btn5 horizontalCenterImageAndTitle:10];
    [self.btn6 horizontalCenterImageAndTitle:10];
    
    // 子这里布局是为了得到self.btn5 的 frame
    [self.view layoutIfNeeded];
    // 为什么这里我又重新进行了更新约束,因为设置玩图片和文字的间距之后,如果不重新布局,图片或者文字会超出按钮的范围。看一下错误的效果图吧。但是我感觉这个地方大家不是特别的会注意到。
    [self.btn5 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.btn5.frame.size.width + 10);
    }];
    [self.btn6 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.btn6.frame.size.width + 10);
    }];

    
    self.btn5.backgroundColor = RandColor;
    self.btn6.backgroundColor = RandColor;

}

错误的图片,大家看一下:


Simulator Screen Shot 2016年12月20日 下午1.41.32.png

附上正确的效果图:


Simulator Screen Shot 2016年12月20日 下午1.35.21.png
上一篇下一篇

猜你喜欢

热点阅读