Any way to bottom-align UILabels with dissimilar fonts?
I have an array of UILabels inside the contentView of a custom UITableViewCell. The font of each label is sized by ranking to form a tag cloud. In the method that sets up the cell (row), I iterate through the word objects that will fit on that line, setting up the frame for each UILabel as follows:
CGRect theFrame = CGRectMake(xPosAdjuster,
theWordRow.rowHeight - thisWord.lblHeight,
thisWord.lblWidth,
thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];
This gets the frames of the labels aligned (see im开发者_开发技巧age below), but, unfortunately, the labels have a padding that is a function of the font size.
Is there any way to remove the padding (border) on a UILabel and/or calculate it exactly so I can adjust the y pos of the frames accordingly?
Thanks
Here is my final code that lines up the labels:
CGRect theFrame = CGRectMake(xPosAdjuster,
floor(theWordRow.rowHeight - thisWord.lblHeight),
floor(thisWord.lblWidth),
thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];
...
CGRect newFrame = myLabel.frame;
newFrame.origin.y -= floor(myLabel.font.descender);
myLabel.frame = newFrame;
You may want to take a look at this page. There is information on Apple's Docs, however this was the first I found.
So it looks like you'll have to do some calculation based on the descender of the UIFont
. You can easily get this value, it is defined as a property on UIFont
.
精彩评论