0

I've really searched for an answer to this one but not come up with anything solid so here goes.

I'm dynamically adding UILabels to a view, they could be any size. I would like the second added one to be placed about 2 pixels below the last regardless whether there is letters that go bellow the line.

I've tried using various method the get the labels frame after it has been sized to font but with no look. The frame always seems to be bigger than the actual text vertically.

Have you guys got any ideas?

Erik
  • 935
  • 11
  • 28
Gaz Long
  • 347
  • 3
  • 13

1 Answers1

1

First, I'd try setting the label's adjustsFontSizeToFitWidth to NO and see if that it will allow you to mess up the normal proportions, as seems to be your goal.

If that doesn't work, you probably need to draw the string to a CALayer. Create a CALayer and add it to your view's root layer, i.e.:

[yourView.layer addSublayer.yourNewLayerForString]. 

Set the sublayer's delegate to the class which will do the drawing:

[yourNewLayerForString setDelegate:self];

And implement drawLayer in that class (the one you assigned as delegate). The drawLayer code can be kind of ugly, but you can find examples out there for how to draw a string in a layer. This one looks good: addTextToLayer.

You will then have exact control over the font and string size, and can adjust the layer frame and the view frame to suit your aims.

Community
  • 1
  • 1
Wienke
  • 3,723
  • 27
  • 40