UIButton的titleEdgeInsets和imageEdgeInsets

首先必须要了解以下四个概念

1.titleEdgeInsets是titleLabel相对于其上下左右的inset,跟》tableView的contentInset是类似的;
2.如果只有title,那titleLabel的 上下左右 都是 相对于Button 的;
3.如果只有image,那imageView的 上下左右 都是 相对于Button 的;
4.如果同时有image和label,那image的 上下左 是 相对于Button 的,右 是 相对于label 的;
label的 上下右 是 相对于Button的, 左 是 相对于label 的。

调整insets的例子,让图片和文字互换位置

更改前

UIButton的titleEdgeInsets和imageEdgeInsets_第1张图片
284517-c8d3f8b8185b5d02.png

更改后

UIButton的titleEdgeInsets和imageEdgeInsets_第2张图片
284517-02e5e32f48c16edb.png

也就是最终我们希望图片上下位置不变top = 0, bottom = 0;向右位移一个label的宽度

同时标题上下位置不变top = 0, bottom = 0;向左位移一个图片的宽度。即 imageEdgeInsets = UIEdgeInsetsMake(0,0 + labelWidth,0,0 - labelWidth); 图片相对初始状态左边距加了labelWidth,右边减labelWidth,以及titleEdgeInsets = UIEdgeInsetsMake(0,0 - imageViewWidth,0, 0 + imageViewWidth);

ls: 我是这样理解的,UIbutton的图片默认insets.right已含有文字的偏移量,而文字默认insets.left也已经默认含有图片的偏移量。因此imageEdgeInsets.left 加了labelWidth之后,imageEdgeInsets.right要减去labelWidth...而titleEdgeInsets同理,不然只加上Inset值而不减的话文字和图片会同时居中

另外,补充一下以下四种情况:(可以写一个分类并且给原有类加一个方法)

.h文件
typedef NS_ENUM(NSUInteger, MKButtonEdgeInsetsStyle) {
    MKButtonEdgeInsetsStyleTop, // image在上,label在下
    MKButtonEdgeInsetsStyleLeft, // image在左,label在右
    MKButtonEdgeInsetsStyleBottom, // image在下,label在上
    MKButtonEdgeInsetsStyleRight // image在右,label在左
};

/**
 *  设置button的titleLabel和imageView的布局样式,及间距
 *
 *  @param style titleLabel和imageView的布局样式
 *  @param space titleLabel和imageView的间距
 */
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space;

.m文件
- (void)layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyle)style
                        imageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0,用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case MKButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case MKButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case MKButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

参考文案,并感谢作者
http://www.jianshu.com/p/3052a3b14a4e
http://www.jianshu.com/p/62850b201049

你可能感兴趣的:(UIButton的titleEdgeInsets和imageEdgeInsets)