iOS UIlabel Autoshrink So Word Doesn't Truncate to Two Lines

iOS UILabel autoshrink so word doesn't truncate to two lines

I can think of nothing that's directly built in. So I'd suggest:

Split the string into components by [NSCharacterSet +whitespaceAndNewlineCharacterSet] and [NSString -componentsSeparatedByCharactersInSet:]. I considered recommending the higher-level NSLinguisticTagger to get out whole words but that wouldn't allow for things like words with a colon on the end.

Of those words, find the typographically largest using the UIKit addition NSString -sizeWithAttributes: (under iOS 7) or -sizeWithFont: (under 6 or below). You're going to make the assumption that the largest will remain the largest as you scale the font size down, which I think will always be true because Apple doesn't do aggressive font hinting.

If that word is already less wide than your view's width, you're done. Just show the string.

Otherwise use a quick binary search, repeatedly querying the size, until you find the smaller font size that you need to within whatever precision you think is appropriate (0.1 of a point sounds reasonable to me but you get the point). Then show the whole string at that size.

UILabel truncate tail and skip not complete word

You can do something like this:

let labelWidth = CGRectGetWidth(label.bounds)

let str = "You will have 30 seconds till you give us a good impression" as NSString
let words = str.componentsSeparatedByString(" ")

var newStr = "" as NSString

for word in words{

let statement = "\(newStr) \(word) ..." as NSString
let size = statement.sizeWithAttributes([NSFontAttributeName:label.font])
if size.width < labelWidth {
newStr = "\(newStr) \(word)"
}
else{
break
}
}

newStr = newStr.stringByAppendingString(" ...")

self.label.text = newStr as String

Idea is: we split words and try check the width while appending from the beginning + the string "..." till we found the a word that will exceed the size, in the case we stop and use this new string

Xcode IB - UILabel multiline text wrapping issue iPhone X + newer models

I found a solution here: iOS UILabel autoshrink so word doesn't truncate to two lines

Essentially, the function determines the length of the longest word in the string and resizes the font to accommodate that word. The only other thing I needed to address was to reset the font size to the original size before calling the function so that strings with shorter words would be adjusted down from the original size instead of using the font size of the previously adjusted string.

UILabel truncate even when it has remaining lines

Use another line breaking mode.

[_detailedTextLabel setLineBreakMode:NSLineBreakByWordWrapping];

and also ensure that label has proper height (given that width is already set):

CGSize size = [_detailedTextLabel sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
_detailedTextLabel.frame = CGRectMake(0, 0, size.width, size.height);

Autoshrink on a UILabel with multiple lines

These people found a solution:

http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/

Their solution is as follows:

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 > 10) */
for(i = maxDesiredFontSize; i > 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 <= 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;

In order to get this working, a IBOutlet must be assigned in the interface builder to the UILabel.

"IBOutlet UILabel *msg;"

All the merit is of the people at 11pixel.

UILabel is not auto-shrinking text to fit label size

In case you are still searching for a better solution, I think this is what you want:

A Boolean value indicating whether the font size should be reduced in order to fit the title string into the label’s bounding rectangle (this property is effective only when the numberOfLines property is set to 1).

When setting this property, minimumScaleFactor MUST be set too (a good default is 0.5).

Swift

var adjustsFontSizeToFitWidth: Bool { get set }

Objective-C

@property(nonatomic) BOOL adjustsFontSizeToFitWidth;

A Boolean value indicating whether spacing between letters should be adjusted to fit the string within the label’s bounds rectangle.

Swift

var allowsDefaultTighteningForTruncation: Bool { get set }

Objective-C

@property(nonatomic) BOOL allowsDefaultTighteningForTruncation;

Source.



Related Topics



Leave a reply



Submit