1、iOSButton图片在上文字在下
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(clickAction:)forControlEvents:UIControlEventTouchUpInside];
[self. addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(0);
make.left.equalTo(self.view.mas_left).offset(15);
make.size.mas_equalTo(CGSizeMake(90 , 86));
}];
btn.tag = i;
btn.titleLabel.font =[UIFont systemFontOfSize:15];
[btn setTitle:titleType[i] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:imageType[i]] forState:UIControlStateNormal];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height+5 ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
[btn setImageEdgeInsets:UIEdgeInsetsMake(-btn.titleLabel.bounds.size.height-5,(btn.frame.size.width-btn.imageView.bounds.size.width)/2.0,0.0,(btn.frame.size.width-btn.imageView.bounds.size.width)/2.0)];
[btn setTitleColor:getColor(@"333333") forState:UIControlStateNormal];
2、iOSButton图片在左文字在右
_btn_address = [AyeClickButton buttonWithType:UIButtonTypeCustom];
_btn_address.frame = CGRectMake(SCREEN_W-50-20, (44-30)/2,60, 30);
//[_btn_address addTarget:self action:@selector(btnAddressAction) forControlEvents:UIControlEventTouchUpInside];
[_btn_address setTitle:_locationAddress forState:UIControlStateNormal];
[_btn_address setTitleColor:getColor(@"#FD7DA8") forState:UIControlStateNormal];
_btn_address.titleLabel.font = [UIFont systemFontOfSize:12.0f];
[_btn_address setImage:[UIImage imageNamed:@"address"] forState:0];
//(正常情况下是文字在右边,图片在左边
//设置图片和文字之间的间隙
_btn_address.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
3、iOSButton图片在右文字在左
_btn_selectTag = [AyeClickButton buttonWithType:UIButtonTypeCustom];
[_btn_selectTag addTarget:self action:@selector(selectTagAction:) forControlEvents:UIControlEventTouchUpInside];
_btn_selectTag.titleLabel.font =[UIFont systemFontOfSize:14];
[_btn_selectTag setTitle:@"请选择" forState:UIControlStateNormal];
[_btn_selectTag setTitleColor:getColor(@"666666") forState:UIControlStateNormal];
[_btn_selectTag setImage:[UIImage imageNamed:@"selectRight"] forState:0];
//使图片在右边,文字在左边(正常情况下是文字在右边,图片在左边)
[_btn_selectTag setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
//设置图片和文字之间的间隙
_btn_selectTag.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
————————————————
版权声明:本文为CSDN博主「码农Lily」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_33552344/article/details/122406892