Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 image below), but, unfortunately, the labels have a padding that is a function of the font size.

alt text

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

like image 510
Bama91 Avatar asked Dec 27 '10 23:12

Bama91


2 Answers

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;

alt text

like image 184
Bama91 Avatar answered Nov 18 '22 15:11

Bama91


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.

like image 28
Joost Avatar answered Nov 18 '22 16:11

Joost