Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These people found a solution:</p> <p><a href="http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/">http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/</a></p> <p>Their solution is as follows:</p> <pre><code>int maxDesiredFontSize = 28; int minFontSize = 10; CGFloat labelWidth = 260.0f; CGFloat labelRequiredHeight = 180.0f; //Create a string with the text we want to display. self.ourText = @"This is your variable-length string. Assign it any way you want!"; /* This is where we define the ideal font that the Label wants to use. Use the font you want to use and the largest font size you want to use. */ UIFont *font = [UIFont fontWithName:@"Marker Felt" size:maxDesiredFontSize]; int i; /* Time to calculate the needed font size. This for loop starts at the largest font size, and decreases by two point sizes (i=i-2) Until it either hits a size that will fit or hits the minimum size we want to allow (i &gt; 10) */ for(i = maxDesiredFontSize; i &gt; minFontSize; i=i-2) { // Set the new font size. font = [font fontWithSize:i]; // You can log the size you're trying: NSLog(@"Trying size: %u", i); /* This step is important: We make a constraint box using only the fixed WIDTH of the UILabel. The height will be checked later. */ CGSize constraintSize = CGSizeMake(labelWidth, MAXFLOAT); // This step checks how tall the label would be with the desired font. CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; /* Here is where you use the height requirement! Set the value in the if statement to the height of your UILabel If the label fits into your required height, it will break the loop and use that font size. */ if(labelSize.height &lt;= labelRequiredHeight) break; } // You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i); // Set the UILabel's font to the newly adjusted font. msg.font = font; // Put the text into the UILabel outlet variable. msg.text = self.ourText; </code></pre> <p>In order to get this working, a IBOutlet must be assigned in the interface builder to the UILabel.</p> <p>"IBOutlet UILabel *msg;"</p> <p>All the merit is of the people at 11pixel.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload