There's ample discussion here on SO about Apple's failure to provide vertical alignment APIs for UILabel
, but how does the actual vertical alignment work? Is the bounding box of the string (cf. sizeWithFont
) centered? Or the x-height of the font (or the cap height?), so that the baseline stays the same regardless of ascenders and descenders? Or what?
Asked
Active
Viewed 1,461 times
2

Community
- 1
- 1

David Moles
- 48,006
- 27
- 136
- 235
1 Answers
0
Here is the code you can use to do vertical alignment of the UILabel. I do not see any apple's failure in providing API to you. You have to use label.transform = CGAffineTransformMakeRotation (-M_PI/2); You have total control on height/width and alignment. Hope this helps..
rect = CGRectMake(0, 5, 130, 30);
label = [[UILabel alloc] initWithFrame:rect];
label.tag =1;
label.font = [UIFont systemFontOfSize:15];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.transform = CGAffineTransformMakeRotation (-M_PI/2);
label.backgroundColor = [UIColor colorWithRed:0.48 green:0.62 blue:0.79 alpha:1.0];
[yourview or self addSubview:label];

Ram G.
- 3,045
- 2
- 25
- 31
-
I believe the OP is talking about the vertical position of the text when drawn "normally" (horizontally) and not the orientation of the text. For one line it is typically in the centre, vertically. – theLastNightTrain Sep 18 '12 at 21:10