美文网首页
UIButton imageEdgeInsets titleEd

UIButton imageEdgeInsets titleEd

作者: Yang152412 | 来源:发表于2016-01-23 17:43 被阅读729次

1、UIButton 的 image 和 title 默认 水平、上下 居中对齐,imageEdgeInsets 和 titleEdgeInsets为 UIEdgeInsetsZero。

2、如果同时设置 image 和 title,则 image 在左,title 在右,中间间距为0。

3、imageEdgeInsets 和 titleEdgeInsets正数为 向内压缩,负数 向外扩张。 相对于 默认情况下的位置。而不是左上角。

tmp26a20bb1.png

4、设置 title 、image 后,可以读到 imageView.frame 和 titleLabel.frame 正确的位置。
但是在 ios9的系统上发现 如果先读取 titleEdgeInsets,则是 0,读不到正确的值,如果先读取 imageEdgeInsets,再读取 titleEdgeInsets,则是正确的(坑爹)。

5、设置 edgeInsets后,再取 imageView.frame 和 titleLabel.frame 就是变化后的值,但是直接设置 frame 却是无效果的。

6、EdgeInsets 的 top 和 bottom,left 和 right 是会相互影响的:如果是一个方向移到,则互相叠加,如果相反,则互相抵消。也就是说都是-5就等于是没有移动了;一个-5一个5就相当于移动10 了。如果要 image 和 title 间隔10,则需要 image 左移5,title 右移5。

7、注意:setTitleEdgeInsets 或是 setImageEdgeInsets, 如果只设置 left or right,则如果想移动10point,则要设置为 X 2,既 20;或者 left 和 right 设置为一正一负(第6点提到的)。
比如,如果想要把 image 移到最左边,则需要 imageView.frame.origion.x * 2 才可以。或者设置 left为 -imageView.frame.origion.x,right 为 imageView.frame.origion.x,既左右都往左移。(为嘛不能设置一边 就可以移到位)

示例1:交换 image 和 title 的位置

    button1.imageEdgeInsets = UIEdgeInsetsMake(0, button1.titleLabel.frame.size.width*2, 0, 0);
    button1.titleEdgeInsets = UIEdgeInsetsMake(0, -button1.imageView.frame.size.width * 2, 0, 0);
    // or
//    button1.imageEdgeInsets = UIEdgeInsetsMake(0, button1.titleLabel.frame.size.width, 0, -button1.titleLabel.frame.size.width);
//    button1.titleEdgeInsets = UIEdgeInsetsMake(0, -button1.imageView.frame.size.width, 0, button1.imageView.frame.size.width);

效果:


button 交换 title 和 image 位置.png

示例2:下面代码 实现 最长的内容居中对齐,间隔10pt. 其他 image 对齐,文本对齐:

- (void)changeButtonPosition {
    NSArray *titles = @[@"location",@"location 34",@"location 123412341234"];
    NSArray *images = @[@"btn_icon_normal",@"btn_icon_normal",@"btn_icon_normal"];
    CGFloat top = 330;
    
    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    CGFloat minLeft = 300/2.0;// 距离 button 左边最小距离,默认居中
    for (int i = 0; i<titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.backgroundColor = [UIColor greenColor];
        button.frame = CGRectMake(15, top, 300, 44);
        button.titleLabel.backgroundColor = [UIColor lightGrayColor];
        button.imageView.backgroundColor = [UIColor yellowColor];
        
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        NSLog(@" \n button imageView frame: %@ , label frame: %@, \n ",
              NSStringFromCGRect(button.imageView.frame),NSStringFromCGRect(button.titleLabel.frame));
        
        CGRect imageViewFrame = button.imageView.frame;
        if (imageViewFrame.origin.x < minLeft) {
            minLeft = imageViewFrame.origin.x;
        }
        
        [buttons addObject:button];
        top += (30 + 44);
    }
    // image 和 title 间距10,image 左移5,title 右移5;
    [buttons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *button = obj;
        CGRect imageViewFrame = button.imageView.frame;
        button.imageEdgeInsets = UIEdgeInsetsMake(0, (minLeft -imageViewFrame.origin.x - 5)*2 , 0, 0);
        
        // title 跟着 image 移动,只不过要少移 5
//        CGRect titleLabelFrame = button.titleLabel.frame;
        button.titleEdgeInsets = UIEdgeInsetsMake(0, (minLeft -imageViewFrame.origin.x + 5)*2 , 0, 0);
        
        NSLog(@" \n button edgeInsets imageView frame: %@ , label frame: %@, \n ",
              NSStringFromUIEdgeInsets(button.imageEdgeInsets),NSStringFromUIEdgeInsets(button.titleEdgeInsets));
        
        NSLog(@" \n button imageView frame: %@ , label frame: %@, \n ",
              NSStringFromCGRect(button.imageView.frame),NSStringFromCGRect(button.titleLabel.frame));
        
        [button setNeedsLayout];
    }];
}

效果:

tmp0814047a.png

相关文章

网友评论

      本文标题:UIButton imageEdgeInsets titleEd

      本文链接:https://www.haomeiwen.com/subject/visrkttx.html