user1620383
user1620383

Reputation: 75

How to set label color and alignment for uibutton dynamically

I am using the following code to set the title label's text color, alignment and font size when adding a ui button in xcode. The font size is picked up but not the text color and alignment, why? Thanks.

toButton.titleLabel.textColor = [UIColor redColor];
toButton.titleLabel.textAlignment = UITextAlignmentRight;
toButton.titleLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
[toButton setTitle:fromButton.titleLabel.text forState:UIControlStateNormal];

Upvotes: 2

Views: 6587

Answers (1)

Midhun MP
Midhun MP

Reputation: 107131

You are setting the color to titleLabel instead of this, use:

[toButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

For text alignment this line will work:

toButton.titleLabel.textAlignment = UITextAlignmentRight;

You can also use:

toButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

Upvotes: 7

Related Questions