代碼實(shí)現(xiàn)按鈕圖片文字上下混排
self.carBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.carBtn setTitle:@"購(gòu)物車" 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);
}];
關(guān)于屬性
setImageEdgeInsets
和setTitleEdgeInsets
設(shè)置圖片及文字內(nèi)邊距,上左下右分別計(jì)算你需要的間距就好
效果圖
button.png