views:

285

answers:

2

Hi Guys, I need to dispaly the email address from left side of a UIButton, But it is getting in the center. Is there any way to set the alignment to the left side of a UIButton.

UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
                emailBtn.backgroundColor=[UIColor clearColor];
                [emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
                emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
                [emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
                [emailBtn addTarget:self  action:@selector(emailAction:)  forControlEvents:UIControlEventTouchUpInside];
                [elementView addSubview:emailBtn];
                [emailBtn release];

Please help me. Thank You, Madan Mohan.

+3  A: 

Set the contentHorizontalAlignment:

emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

You might also want to adjust the content left inset otherwise the text will touch the left border:

emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
DyingCactus
A: 

you can always create a label with left alignment and add that label to the button... something like

CGRect buttonRect = emailBtn.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: buttonRect];
myLabel = UITextAlignmentLeft;
[emailBtn addSubview:myLabel];
[myLabel release];
Digital Robot