文字在左,图片在右:
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;
效果如下:
上下布局等其他类似~