btn.frame = CGRectMake(x, y, width, height);app
[btn setTitle: @"search" forState: UIControlStateNormal];ide
//设置按钮上的自体的大小字体
//[btn setFont: [UIFont systemFontSize: 14.0]]; //这种能够用来设置字体的大小,可是可能会在未来的SDK版本中去除改方法spa
//应该使用3d
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];orm
[btn seBackgroundColor: [UIColor blueColor]];it
//最后将按钮加入到指定视图superViewio
[superView addSubview: btn];class
==========================================================plugin
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
这样初始化的button,文字默认颜色是白色的,全部若是背景也是白色的话,是看不到文字的,
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
有些时候咱们想让UIButton的title居左对齐,咱们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有做用的,咱们须要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
可是问题又出来,此时文字会紧贴到作边框,咱们能够设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离作边框保持10个像素的距离。
=======================================================
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];