Venkat Venkat - 4 days ago 6
iOS Question

Change the color and alignment of a UIButton's text

I am creating a

UIButton
in a
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];


The button is working fine......... but the button text is in blue color and center-aligned. I want to change the text color and alignment - how do I do this?

Answer

Set button title color & Alignment ...

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

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter]; 

or

aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;

or

[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
Comments