iOS 实现UIButton的文字和图片上下/左右显示

释放双眼,带上耳机,听听看~!

文字在左,图片在右:

UIButton *detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
detailBtn.frame = CGRectMake(50, 50, 100, 50);
detailBtn.backgroundColor = [UIColor cyanColor];
[detailBtn setTitle:@"详情" forState:UIControlStateNormal];
[detailBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
detailBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[detailBtn setImage:[UIImage imageNamed:@"icon_list_箭头@2X"] forState:UIControlStateNormal];
[self.view addSubview:detailBtn];
// 重新设置位置,要写在按钮设置图片和文字之后,否则无效
detailBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -detailBtn.imageView.bounds.size.width-6, 0, detailBtn.imageView.bounds.size.width+6);
detailBtn.imageEdgeInsets = UIEdgeInsetsMake(0, detailBtn.titleLabel.bounds.size.width, 0, -detailBtn.titleLabel.bounds.size.width);
// 居右
detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

效果如下:

image.png

上下布局等其他类似~

人已赞赏
iOS文章

IOS 自定义转场动画

2020-1-7 7:45:33

iOS文章

iOS开发屏幕旋转功能

2020-1-7 9:41:51

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索