1 | btn.frame = CGRectMake(x, y, width, height); |
1 | tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)]; |
这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,
1 | btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中 |
有些时候我们想让UIButton的title居左对齐,我们设置
1 | btn.textLabel.textAlignment = UITextAlignmentLeft |
是没有作用的,我们需要设置
1 | btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft; |
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
1 | btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0); |
使文字距离做边框保持10个像素的距离。
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
1 | [btn.titleLabel setTextColor:[UIColorblackColor]]; |
而是用:
1 | [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal]; |
资源参考:http://blog.csdn.net/chengyingzhilian/article/details/8363855