8

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

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Bama91
  • 884
  • 2
  • 10
  • 24

2 Answers2

10

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

Bama91
  • 884
  • 2
  • 10
  • 24
7

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.

Joost
  • 10,333
  • 4
  • 55
  • 61
  • for those of you having issues with the descender, you can edit your font file as described here: http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/ – cohen72 Jan 29 '13 at 17:39