让button上左右顺序显示 文字和图片,需要用到 UIButton的setImage 方法 ,而不是setBackgroundImage方法
UIButton*btn= [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@“标题”forState:UIControlStateNormal] ; // 添加文字
设置UIButton的title居左或者右对其,用contentHorizontalAlignment属性,其为枚举类型,可根据需要自行设置,例如:
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
切记无效代码:
btn.textLabel.textAlignment = UITextAlignmentLeft;
此时文字会紧贴边框,视觉效果不好,可以用 contentEdgeInsets属性调整间距,例如:
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
字体颜色用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
无效代码:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
字号大小:
btn.titleLabel.font=[UIFont boldSystemFontOfSize:16];