iOS 秒懂 button 的 setTitleEdgeInsets 和 setImageEdgeInsets


Demo地址
Demo中包含本人学习的所有资料,还有一些封装的Pod组件,欢迎下载Star,如果有错误的地方,还请指出,详情查看 README.md


开发了这么多年,每次遇到调整 Button 的图片和文字位置的问题,都要去搜索,然后各种尝试,解决方案虽然有,但是没有一篇文章给我说明白是怎么回事的。

这种事情还是需要自己来啊,终于解了多年的疑惑,居然如此简单


以下仅针对
button 的 setTitleEdgeInsets 和 setImageEdgeInsets 方法

效果图
图一

原理

/**
 * UIEdgeInsetsMake(上, 左, 下, 右)
 * 偏移参照物为自己,初始偏移量是0
 * 偏移量计算公式:
 * offsetX = (左-右)/2 ,为正 则向右偏移,为负则向左偏移
 * offsetY = (上-下)/2 ,为正 则向下偏移,为负则向上偏移
 *
 * 最终偏移量是根据最终算出来的数值决定 (offsetX,offsetY)
 */

就是这么简单
如果我们要将文字向右偏移 5个点,以下四种结果相同,
(左-右) / 2 = offsetX = 5

// 第一种
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0,0)];

// 第二种
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0,-10)];

// 第三种
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0,-25)];

// 第四种
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0,-5)];

源码中:Demo实例 ,效果图为 图一

-(void)setUpSubViews{
    
    UIButton *btn = [[UIButton alloc]init];
    btn.backgroundColor = [UIColor lightGrayColor];
    [btn setTitle:@"居左button" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.right.offset(-100);
        make.top.offset(STATUSNAVIBAR_HEIGHT + 100);
        make.height.mas_equalTo(80);
    }];
    
    
    UIButton *btn1 = [[UIButton alloc]init];
    btn1.backgroundColor = [UIColor lightGrayColor];
    [btn1 setTitle:@"居右button" forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.right.offset(-100);
        make.top.equalTo(btn.mas_bottom).offset(50);
        make.height.mas_equalTo(80);
    }];
    
    
    UIButton *btn2 = [[UIButton alloc]init];
    btn2.backgroundColor = [UIColor lightGrayColor];
    [btn2 setTitle:@"左上button" forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.right.offset(-100);
        make.top.equalTo(btn1.mas_bottom).offset(50);
        make.height.mas_equalTo(80);
    }];
    
    
    UIButton *btn3 = [[UIButton alloc]init];
    btn3.backgroundColor = [UIColor lightGrayColor];
    [btn3 setTitle:@"右下button" forState:UIControlStateNormal];
    [self.view addSubview:btn3];
    
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.right.offset(-100);
        make.top.equalTo(btn2.mas_bottom).offset(50);
        make.height.mas_equalTo(80);
    }];
    
    
    UIButton *btn4 = [[UIButton alloc]init];
    btn4.backgroundColor = [UIColor lightGrayColor];
    [btn4 setTitle:@"带图IMAGE" forState:UIControlStateNormal];
    [btn4 setImage:[UIImage imageNamed:@"time"] forState:UIControlStateNormal];
    [self.view addSubview:btn4];
    
    [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(100);
        make.right.offset(-100);
        make.top.equalTo(btn3.mas_bottom).offset(50);
        make.height.mas_equalTo(80);
    }];
    

    ///===================================///
    ///============开始测试偏移量===========///
    ///===================================///
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];
    
    //图片文字宽度
    CGFloat textW = btn.titleLabel.intrinsicContentSize.width;
    CGFloat btnW = btn.width;
    CGFloat offsetX = (btnW - textW)/2;
    
    //图片文字高度
    CGFloat textH = btn.titleLabel.intrinsicContentSize.height;
    CGFloat btnH = btn.height;
    CGFloat offsetY = (btnH - textH)/2;
    
    
    /// 偏移量计算:(0-offsetX * 2)/2 = -offsetX
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, offsetX * 2)];
    
    ///常规对称写法:数值相同,一正一负
//    /// 偏移量计算:(offsetX- (-offsetX))/2 = offsetX
//    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, offsetX, 0, -offsetX)];
    
    /// 非对称数值测试
    /// 偏移量计算:((offsetX+20) - (-offsetX + 20))/2 = offsetX
    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(10, -1000, 2, -1000-offsetX*2)];
    
    [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, offsetY * 2, offsetX * 2)];
    [btn3 setTitleEdgeInsets:UIEdgeInsetsMake(offsetY, offsetX, -offsetY, -offsetX)];
    
    CGFloat imgW = btn4.imageView.intrinsicContentSize.width;
    CGFloat textW1 = btn4.titleLabel.intrinsicContentSize.width;
    
    /// 图文:设置文字图片调换位置,且图片文字间隔 10 个点
    [btn4 setTitleEdgeInsets:UIEdgeInsetsMake(0, -(imgW + 5)*2, 0, 0)];
    [btn4 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0,  -(textW1 + 5)*2)];
}

你可能感兴趣的:(iOS 秒懂 button 的 setTitleEdgeInsets 和 setImageEdgeInsets)