How can I programmatically set the vertical alignment of a UIButton - iOS

The problem was the using

button.titleLabel.textColor = [UIColor grayColor];

instead of

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

Elsewhere in my code I am still having troubles vertically aligning the text, but I have got the basic example going, so I should be able to get the more complicated version going if I look at it more. Perhaps there are other methods I am using like button.titleLabel.textColor which make the vertical alignment not work...

Update The original problem was actually because my button was too short (button height).

The word `husband' is in the button

The word husband is in the button, which is currently vertically aligned to the bottom. But since the height of the button is not enough, it is not obvious. Even though the default of the title insets and content insets are zero, it does not seem like the title can be flush against the bottom. It looks like about 5 pixels to me. I tested with a smaller font to make sure this is correct.

Unfortunately vertically aligning the text to the top does not seem to change anything...


contentVerticalAlignment is the way to go. Could be how you're creating the button. Try this:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 button.frame = CGRectMake(10, 10, 110, 130);
 // rest of your code
 // don't release button explicitly

You can alignment (horizontal and vertical) the text of button in this way:

        UIButton *button  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 70)];
        button.backgroundColor = [UIColor whiteColor];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        [button setTitle:@"Align" forState:UIControlStateNormal];
        [self.container addSubview:button];