Venkat Venkat - 9 months ago 61
iOS Question

Change the color and alignment of a UIButton's text

I am creating a

in a

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 Source

Set button title color & Alignment ...

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

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter]; 


aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;


[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];