iOSButton文字和图片摆放的个字方式

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值