您的位置:首页 > 产品设计 > UI/UE

iOS _ UIButton 标题字体大小颜色位置等

2016-01-29 10:42 357 查看
(void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

button.frame = CGRectMake(x, y, width, height);

[button setTitle: @”点我” forState: UIControlStateNormal];

//设置按钮上的自体的大小

//[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

//应该使用

button.titleLabel.font = [UIFont systemFontOfSize: 14.0];

[button seBackgroundColor: [UIColor blueColor]];

//最后将按钮加入到指定视图superView

[superView addSubview: button];

//==========================================================

tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

//这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

[button setTitle:@”标题”forState:UIControlStateNormal];// 添加文字

//有些时候我们想让UIButton的title居左对齐,我们设置

button.textLabel.textAlignment = UITextAlignmentLeft

//是没有作用的,我们需要设置

button.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

//但是问题又出来,此时文字会紧贴到做边框,我们可以设置

button.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

//使文字距离做边框保持10个像素的距离。

//=======================================================

//设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

[button.titleLabel setTextColor:[UIColor blackColor]];

button.titleLabel.textColor=[UIColor redColor];

//而是用:

[button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uibutton