Button图文上下布局__iOS开发

代码实现按钮图片文字上下混排

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);
    }];

关于属性

setImageEdgeInsetssetTitleEdgeInsets设置图片及文字内边距,上左下右分别计算你需要的间距就好

效果图

button.png

你可能感兴趣的:(Button图文上下布局__iOS开发)