Black20
Black20

Reputation: 437

Dynamically determine size of font based on UILabel frame size

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

Upvotes: 0

Views: 5292

Answers (3)

NANNAV
NANNAV

Reputation: 4901

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];

Upvotes: 5

morningstar
morningstar

Reputation: 9162

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.

Upvotes: 0

morningstar
morningstar

Reputation: 9162

adjustsFontSizeToFitWidth

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

Upvotes: 1

Related Questions