Tuesday, 24 April 2012

iPhone | UIButton set title label, font, alignment

     
UIButton *yourButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
yourButton.frame=CGRectMake(X_ORIGIN,Y_ORIGIN,WIDTH,HEIGHT);
// Here, yourButton.titleLabel.text = @"YOUR_TEXT"; // doesn't do the trick :(
[yourButton setTitle:@"YOUR_TEXT" forState:UIControlStateNormal];
[yourButton addTarget:self action:@selector(YOUR_SELECTOR) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: yourButton];

However, I'm not sure why yourButton.titleLabel.text = @"YOUR_TEXT";  doesn't work
[yourButton setTitle:@"YOUR_TEXT" forState:UIControlStateNormal]; did the trick though!

if you change the font of this title :
yourButton.titleLabel.font = YOUR_FONT_VALUE;

if you change the alignment-left of UIButton :
yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
adjustment of content left inset , otherwise text will touch the left border.
yourButton.contentEdgeInsets = UIEdgeInsetsMake(0,10,0,0);

Note: Check out UIControl API docs.

Hope it makes life easier!

No comments:

Post a Comment