1

I am trying to solve a problem that i've looked around for but can't figure out. I have a UILabel control inside of a UIScrollView control. I have set the height of the label to the height of the scrollview in which it is contained, now I need to determine the correct height (dynamically) of text that will fill the label. So whenever the scrollView height is, the text height inside of the label will match and the width will expand to prevent the text from truncating (like the image below).

I've tried things like sizeToFit, constrainedSize, but i couldn't get it to work properly, any direction on how to achieve something like this?

enter image description here

Prateek Prem
  • 1,544
  • 11
  • 14
Black20
  • 347
  • 1
  • 5
  • 10
  • Which part are you actually having trouble with? Setting the font size or setting the width? – borrrden Jun 10 '13 at 03:16
  • See my Ans on this link [UILabelExtended](http://stackoverflow.com/questions/16868298/how-to-adjust-a-label-size-to-fit-the-length-of-the-text/16868554#16868554) > You can create `UILabelExtended` class copy and paste from the link and use that. – Prateek Prem Jun 10 '13 at 03:33
  • I am trying to find the largest font size (constrained by height) that will fit fill (height wise) the frame. Essentially I want the text to fill the scrollview height wise. SizeToFit shrinks the height too much. – Black20 Jun 10 '13 at 12:08

3 Answers3

5

Try this simple code,it's helps you

 UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    label.backgroundColor=[UIColor clearColor];
    label.text= yourString;
    label.numberOfLines=0;
    label.adjustsFontSizeToFitWidth=YES;
    label.minimumFontSize=6;
    label.textAlignment=UITextAlignmentCenter;
    [self.View addSubview:label];

    CGFloat fontSize = 20;
    while (fontSize > 0.0)
    {
        CGSize size = [yourString sizeWithFont:[UIFont fontWithName:@"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];

        if (size.height <= label.frame.size.height) break;

        fontSize -= 1.0;
    }

    //set font size
    label.font = [UIFont fontWithName:@"Rockwell-Bold" size:fontSize];
    [label release];
NANNAV
  • 4,875
  • 4
  • 32
  • 50
1

adjustsFontSizeToFitWidth

In Interface Builder it's the "Adjust to Fit" checkbox.

morningstar
  • 8,952
  • 6
  • 31
  • 42
0

I noticed you want to make the text fill the full width and the full height. It is probably difficult to talk the text into doing that. There is no way to set the exact bounding rectangle of text directly. You can only set the font as a point size; it will scale the text with more or less a fixed aspect ratio.

You should be able to achieve what you want with the label's transform property. First, build a label of a fixed size and choose a font so that the text fills the frame. (If your text will vary, it's a little more complicated. Use [text sizeWithFont:font] to get the right size, and set the label frame from that.)

Say your label size does not match your scroll view size. E.g. the label is 100x10 and the scrollview is 200x15. If you set your label's frame to 200x15, you will not get the effect you want. The label will get bigger but the text inside it will stay the same size. However, if you do this

label.transform = CGAffineTransformMakeScale(2.0, 1.5);

then your whole label and its contents will get stretched. You might also need to adjust the label's center property to make it positioned correctly within the parent view. You will need to have your code do some calculations to get this right every time.

morningstar
  • 8,952
  • 6
  • 31
  • 42