1. button上添加背景图片是蓝色的
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; // sysytem 自带渲染效果,就是有种按下去的感觉
UIImage *image = [UIImage imageNamed:@"5ZPU059BYTUY)CE9ZIKWEPN.jpg"];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(40, 40, 100, 100);
[self.view addSubview:button];
将红色的那句代码改成
// [button setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal]; // 改变图片的渲染方式,是原始的, 就不会受系统的影响了
2. 设置button上按钮字体大小
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
有些时候我们想让UIButton的title居左对齐,我们设置
btn.titleLabel.textAlignment = UITextAlignmentLeft;
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
4. 设置按钮上字体的颜色
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];