代码实现按钮图片文字上下混排
self.carBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.carBtn setTitle:@"购物车" forState:UIControlStateNormal];
self.carBtn.titleLabel.font = [UIFont systemFontOfSize:11];
[self.carBtn setTitleColor:[UIColor colorWithHexString:@"505050" alpha:1] forState:UIControlStateNormal];
[self.carBtn setImage:[UIImage imageNamed:@"cart"] forState:UIControlStateNormal];
[self.carBtn setBackgroundColor:[UIColor whiteColor]];
[self.carBtn setImageEdgeInsets:UIEdgeInsetsMake(-15, 22,0, -self.carBtn.titleLabel.bounds.size.width)];
[self.carBtn setTitleEdgeInsets:UIEdgeInsetsMake(self.carBtn.imageView.frame.size.height+10 ,-self.carBtn.imageView.frame.size.width+10, 0,0)];
[self.carBtn addTarget:self action:@selector(toCarVC) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.carBtn];
[self.carBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.top.equalTo(self);
make.width.offset(80);
}];
关于属性
setImageEdgeInsets
和setTitleEdgeInsets
设置图片及文字内边距,上左下右分别计算你需要的间距就好