Vertically align UILabel
Posted
by
Ashish Agarwal
on Stack Overflow
See other posts from Stack Overflow
or by Ashish Agarwal
Published on 2012-06-29T10:16:12Z
Indexed on
2012/07/03
15:16 UTC
Read the original article
Hit count: 211
I am trying to vertically align the text in the UILabel view of my app. The problem is that I want the text to be vertically aligned to the top and the size of the label to be 280 x 150. I am only able to achieve one of these 2 things. If I remove the line
[myLabel sizeToFit];
then the alignment of the text is alright but the size is messed up. But if I add the above line, then the alignment is messed up but the size is alright. How do I fix this problem.
I've added the code below -
CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
© Stack Overflow or respective owner