UIButton图片左文字右

UIButton *watchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[watchBtn setImage:[UIImage imageNamed:@"icon_watch_play"] forState:UIControlStateNormal];
[watchBtn setTitle:ACLocalizedString(@"watch", nil).uppercaseString forState:UIControlStateNormal];
//图片在左 文字在右 间距为5
watchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -2.5, 0, 2.5);
watchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2.5, 0, -2.5);
[watchBtn setTitleColor:RGB_HEX(0xD1D1D1) forState:UIControlStateNormal];
[watchBtn.titleLabel setFont:[UIFont systemFontOfSize:11.0 weight:UIFontWeightBold]];
//内容再设置左右内间隔
watchBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
watchBtn.backgroundColor = RGB_HEX(0x474747);
watchBtn.layer.cornerRadius = 4.0;

//约束设置不拉伸不压缩 随文字长而长
[watchBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[watchBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[watchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
}];

参考
https://juejin.cn/post/6844903566616854541

你可能感兴趣的:(UIButton图片左文字右)