I know how to set the vertical alignment of a button with IB; see here. But I can not do it programmatically… This is one of the things I have tried:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 110, 130)];
[button setTitle:@"Align" forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.textColor = [UIColor grayColor];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[self.view addSubview:button];
[button release];
I have also tried insets…
But the alignment won’t change.
Please note that I want to keep the CustomStyle of the button (I don’t want the default look and feel).
The problem was the using
instead of
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
husbandis 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…