更改UIButton文本的颜色和对齐方式

时间:2012-09-07 05:00:52

标签: iphone ios ipad uibutton

我正在UIButton中创建UITableView

UIButton *aboutButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[aboutButton setTitle:@"ABOUT" forState:UIControlStateNormal];
[aboutButton addTarget:self action:@selector(aboutButtonClicked) forControlEvents: UIControlEventTouchUpInside];
aboutButton.frame=CGRectMake(20, 375, 200, 35);
[tableView addSubView aboutButton];

按钮工作正常.........但按钮文字为蓝色并居中对齐。我想更改文本颜色和对齐方式 - 我该怎么做?

3 个答案:

答案 0 :(得分:2)

设置按钮标题颜色&对齐......

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

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter]; 

aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;

[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

答案 1 :(得分:1)

[aboutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

已在SO中多次询问

button alignment question

another button alignment question

button title color question

another button title color question

答案 2 :(得分:0)

您可以使用-[myButton setTitleColor:forState:]来设置标题的颜色。 可以使用[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]

更改对齐方式