UIEdgeInsets —>titleEdgeInsets
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.backgroundColor = [UIColor blueColor];
button.frame = CGRectMake(50, 100, 200, 40);
[button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[button setTitle:@"返回" forState:(UIControlStateNormal)];
[button setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
[self.view addSubview:button];
button.titleLabel.backgroundColor = [UIColor redColor];
// 这里必须使用titleRectForContentRect这个方法
// 之前使用button.titleLabel.frame时,size是0,x值也不准确,不知道为什么(好像多了图片的宽度)
CGRect tmpRect = [button titleRectForContentRect:button.bounds];
CGFloat width = tmpRect.size.width;
CGFloat label_X = tmpRect.origin.x;
/**
UIEdgeInsetsMake
@param top 距离顶部偏移量
@param left 距离左边偏移量 正数x表示:距离左边距离增加x,即向下偏移 x / 2.0,还不知道为什么会这样,其他同理
@param bottom 距离底部偏移量 只测试了左右,上下没测,应该同理
@param right 距离右边偏移量
@return UIEdgeInsets
*/
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, (200.0 - width - label_X) * 2, 0, 0)];