1

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?

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Venk
  • 5,949
  • 9
  • 41
  • 52
  • 1
    https://www.google.co.in/search?q=uibutton+text+color&sugexp=chrome,mod=18&sourceid=chrome&ie=UTF-8#hl=en&gs_nf=1&tok=X16lTA4rR-i6Vx88X1hsGA&pq=uibutton%20text%20color&cp=15&gs_id=1p&xhr=t&q=uibutton+text+alignment&pf=p&safe=active&sclient=psy-ab&oq=uibutton+text+a&gs_l=&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=6fc3f1821ef6f8e6&biw=1127&bih=986 – Aravindhan Sep 07 '12 at 05:02
  • 1
    https://www.google.co.in/search?q=uibutton+text+color&sugexp=chrome,mod=18&sourceid=chrome&ie=UTF-8#hl=en&safe=active&sclient=psy-ab&q=uibutton+title+color&oq=uibutton+title+color&gs_l=serp.3..0j0i30l3.54528.57959.3.58134.13.9.1.3.3.3.1674.7772.4-1j2j2j1j2.8.0...0.0...1c.1.8KcagvrnVw0&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=6fc3f1821ef6f8e6&biw=1127&bih=986 – Aravindhan Sep 07 '12 at 05:03
  • This really should could been solved with a click glance at the `UIButton` and `UILabel` class references, there was no need to post this on SO. You can view the docs in Xcode or on the web, they are quite straightforward and detailed. – Carl Veazey Sep 07 '12 at 05:24

3 Answers3

2

Set button title color & Alignment ...

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

[aboutButton.titleLabel setTextAlignment:UITextAlignmentCenter]; 

or

aboutButton.titleLabel.textAlignment = UITextAlignmentCenter;

or

[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
1
[aboutButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]
[aboutButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

it has been asked several times in SO

button alignment question

another button alignment question

button title color question

another button title color question

Community
  • 1
  • 1
janusfidel
  • 8,036
  • 4
  • 30
  • 53
0

You use -[myButton setTitleColor:forState:] for setting the color of the title. The alignment can be changed with [myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]

Legolas
  • 12,145
  • 12
  • 79
  • 132