iphone - How to adjust font size of label to fit the rectangle? -


Yes, this is good myLabel.adjustsFontSizeToFitWidth = YES; Property But as soon as the label has two lines or more, it will not resize the text. So it gets smaller with just ... if it does not fit in the reset.

Is there another way to do this?

If you want to make sure that the label fits both width and height in the rectangle, then you Want to see if anyone will fit.

This snippet starts at 300 times and attempts to fit the label in the targeted rectangle by reducing the font size.

  - (zero) size label: Reference to label (UILabel *): (CGRect) Label line {// Set the label of the frame to the target rectangle label. Frame = label; // Size of all fonts from largest to small font size int fontSize = 300; Int minFontSize = 5; // Fit label width wig CGSize constraintize = CGSizeMake (label.frame.size.width, MAXFLOAT); Do {// set current font size label.font = [UIFont fontWithName: label.font.fontName Size: fontSize]; // Find the size of the label for the current font size CGRTT TextActact = [[Labels Text] BoundingRightWith Size: Handicap Options: NSSTTINGDRAWINGWEENLINEFragmentAgreen Features: @ {NSFontAttributeName: label.font} Reference: Zero]; CGSize label size = textRect.size; // Ready, break if the made label is within the target size (labelSize.height & lt; = label.frame.size.height); // reduce font size and try again fontSize - = 2; } While (fontSize & gt; minfontsis); }  

Comments