UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, <span style="color:#FF0000;">71</span>, 28);
btn.backgroundColor = [UIColor blackColor];
[btn setTitle:@"更多" forState:UIControlStateNormal];
btn.layer.cornerRadius = 5;
btn.layer.masksToBounds = YES;
btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
//14*14<span id="transmark"></span> 这个是图片大小
[btn setImage:[UIImage imageNamed:@"white_thum<span id="transmark"></span>b.png"] forState:UIControlStateNormal];
// btn.imageView.backgroundColor = [UIColor yellowColor];
// btn.titleLabel.backgroundColor = [UIColor blueColor];
[self.view addSubview:btn];
CGSize imageSize = btn.imageView.frame.size;
//titleSize 有一个神奇的事情 那就是 如果 btn.titleLabel.backgroundColor 有赋颜色值 那么titleSize 才会有值,否则是{0,0}
//所以下面 我注释了,计算的时候 就用到了 btn.titleLabel.frame.size.width, 而不是titleSize.width</span>
// CGSize titileSize = btn.titleLabel.frame.size;
//先算出两边的间距
CGFloat abc = (btn.frame.size.width - btn.titleLabel.frame.size.width - imageSize.width ) / 2;
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, imageSize.width + abc)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, abc + btn.titleLabel.frame.size.width, 0, 0)];
下面是 前后对比
偏移之前
偏移之后