0

I need to add a variable number of UIViews to a ViewController; the UIView contains a string that is drawn using CoreText. I am currently adding a single UIView by doing:

    CGRect frame;
    frame.origin.x = x;
    frame.origin.y = y;
    frame.size.width = 100;
    frame.size.height = 100;

    CTLabelView *label=[[CTLabelView alloc]  initWithFrame:frame];
    [label setLabelText:@"my string"];

I would like to avoid to specify the frame, letting the label to occupy the space it needs according to its length. Which is the best way to do it avoiding to specify width and height?

Cris
  • 11,540
  • 26
  • 83
  • 155
  • 1
    Looks like there is another question very similar http://stackoverflow.com/questions/8796862/xcode-uilabel-auto-size-label-to-fit-text – Srikanth Dec 08 '12 at 22:22

1 Answers1

2

You can get string size for specified font.

// get your font
UIFont *font = label.font;

// Find width of a string ...
CGSize size = [@"my string" sizeWithFont:font];
Oguz Demir
  • 159
  • 2
  • 6