Auto resize the text size of a MULTILINE UILabel - ios

I want to auto resize the text size of a UILabel with numberOfLines>1. The UILabel width and height have to be fix. Is there a better solution instead of counting the characters and setting the size manualy? I am using iOS6.

I modified a solution I found. This can be used now in a UITableViewCell:
- (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize lblWidth: (float)lblWidth lblHeight: (float)lblHeight {
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
// start with maxSize and keep reducing until it doesn't clip
for(int i = maxSize; i >= minSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(lblWidth, MAXFLOAT);
// NSString* string = [aLabel.text stringByAppendingString:#"..."];
CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:aLabel.lineBreakMode];
if(labelSize.height <= lblHeight)
break;
}
// Set the UILabel's font to the newly adjusted font.
aLabel.font = font;
}

The below line of code will give you the full size of the string:
CGSize labelSize = [labelString sizeWithFont:label.font constrainedToSize:label.contentSize];

Related

How to dynamically resize text size in a UITextView on iPhone 4S Swift

My app shows a string array that iterates through by button press. It works perfectly on all devices except iPhone 4S. The problem is some string elements are to long to fit within the UITextView and you have to scroll to read the rest of it, which I do not want.
So, my question is how to dynamically shrink attributed text to fit within a constrained UITextView when it doesn't fit all the string element?
Let me know if this is possible,
Thanks guys.
Why are you using UITextView, when you can use UILabel for you situation. UITextView does not have autoscaling property. With UILabel you can set number of lines and it has Autoshrinkproperty where you can set Minimum font scale or Minimum font size that will adjust your text accordingly to UILabel size. Or just allow scrolling with UITextView.
Use this in your textview category class to fit font size depend upon width.
-(BOOL)sizeFontToFit:(NSString*)aString minSize:(float)aMinFontSize maxSize:(float)aMaxFontSize
{
float fudgeFactor = 16.0;
float fontSize = aMaxFontSize;
self.font = [self.font fontWithSize:fontSize];
CGSize tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
CGSize stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
while (stringSize.height >= self.frame.size.height)
{
if (fontSize <= aMinFontSize) // it just won't fit
return NO;
fontSize -= 1.0;
self.font = [self.font fontWithSize:fontSize];
tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
}
return YES;
}

Why is so much extra space being added to my UILabel when I try to change height to fit text?

I have no idea why the height of my UILabel is expanding to such a great height. It leaves the text of my UILabel in the centre. I don't want all of this extra space...
Unwanted extra space
Here is my code (I set the text before this point):
self.infoDescription.numberOfLines = 0;
[self.infoDescription sizeToFit];
self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
Please help :( I just want the height of the UILabel to fit the text exactly.
First set the frame and then make size to fit of label.
**self.infoDescription.frame = CGRectMake(20, self.infoAdultSize.frame.size.height+self.infoAdultSize.frame.origin.y+10, self.infoView.frame.size.width-40, self.infoDescription.frame.size.height);
[self.infoDescription sizeToFit];**
Try this:
CGSize maximumSize = CGSizeMake(300, 9999);
NSString *myString = #"This is a long string which wraps";
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize myStringSize = [myString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:self.myLabel.lineBreakMode];
(original source)
Please check this answer it works for me and it is exactly what you are looking for.
//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;

iOS 8 boundingRectWithSize Calculates Wrong Text Size

I have a string thats length will change. I have a textView with a fixed width and height. I want to find the maximum font size that will fit within these bounds. I use boundingRectWithSize. I assume once it gives me a value larger then my testRect I can lower the font and I should be good. But the string is too large for my textView. I always have to lower it by 2-4 points even though the testRect.size.height is lower then my textView.frame.size.height.
NSString *testString = #"This is a test string. Does it get biger?";
CGFloat height = self.textView.frame.size.height;
CGFloat width = self.textView.frame.size.width;
CGSize testSize = CGSizeMake(width, height);
CGFloat fontSize = 15;
NSDictionary *attrsDictionary;
CGRect testRect;
int i = 0;
while (i == 0) {
attrsDictionary = #{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:fontSize]};
testRect = [testString boundingRectWithSize:testSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrsDictionary context:nil];
if (testRect.size.height < testSize.height) {
fontSize++;
} else {
fontSize--;
self.textView.font = [UIFont fontWithName:#"Helvetica" size:fontSize];
self.textView.text = testString;
i = 1;
}
}
When you have a flawless font your approach should work.
Unfortunately flawless fonts are rare, even system fonts usually aren't.
This means you have to find the required adjustments for your font yourself ...

Manage label text dynamically not work properly

CGFloat constrainedSize = 500.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19]; //or any other font that matches what you will use in the UILabel
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];
lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10,y, 300,textSize.height)];
i have tried this code for manage the dynamic text. but if the data comes larger it will not display the whole text.
You constrain the size to width = 500pt, but your textfield is only 300pt wide.
Edit:
It seems, I wasn't clear. If you calculate the height of the label with sizeWithFont and give as constraint a width of 500pt (constrainedSize) and use the calculated height then on a label with only 300pt width, the calculated height is not correct.
This is how it works for me:
CGFloat constrainedSize = 300.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19]; //or any other font that matches what you will use in the UILabel
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:NSLineBreakByWordWrapping];
UILabel* lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10.0, 10.0, constrainedSize, textSize.height)];
lblDescription.lineBreakMode = NSLineBreakByWordWrapping;
lblDescription.numberOfLines = 0;
lblDescription.font = myFont;
lblDescription.text = myText;
Again: use the same attributes for the label (font, size, lineBreakMode) as you use for the calculation, otherwise it won't fit.
That's because you are alloc and initing the UILabel, and never reset it's frame for bigger size text.
Simply set the frame of the UILabel after a text size change.
This line sets the size of the label, to textSize.height and it doesn't change.
lblDescription=[[UILabel alloc]initWithFrame:CGRectMake(10,y, 300,textSize.height)];
You can call setFrame to programmatically change this.
To display a larger text/Multiline text you should use UITextView instead. It is designed to handle this issue.
hi here is code to set dynamic text in label
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [label.text sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:label.lineBreakMode];
CGRect newFrame = label.frame;
newFrame.size.height = expectedLabelSize.height;
label.frame = newFrame;
i hope this is helping to you..

How can I compute the number of lines of a UILabel with a fixed width? [duplicate]

This question already has answers here:
How to find UILabel's number of Lines
(8 answers)
Closed 9 years ago.
How can I compute the number of lines of a UILabel with a fixed width and a given text ?
This code assumes label has the desired text and its frame is already set to the desired width.
- (int)lineCountForLabel:(UILabel *)label {
CGSize constrain = CGSizeMake(label.bounds.size.width, FLT_MAX);
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:constrain lineBreakMode:UILineBreakModeWordWrap];
return ceil(size.height / label.font.lineHeight);
}
Update:
If all you want is to determine the required height for the label based on its text and current width, then change this to:
- (CGSize)sizeForLabel:(UILabel *)label {
CGSize constrain = CGSizeMake(label.bounds.size.width, FLT_MAX);
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:constrain lineBreakMode:UILineBreakModeWordWrap];
return size;
}
The returned size is the proper width and height to contain the label.
First get the height of the label from the label size using constrainedSize
CGSize labelSize = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGFloat labelHeight = labelSize.height;
once you have the label height then check the number of lines with the font size where fontsize is the size you are using for your label. e.g it could be 10 or depending on your requirements
CGSize sizeOfText = [label.text sizeWithFont:label.font
constrainedToSize:label.frame.size
lineBreakMode:UILineBreakModeWordWrap];
int numberOfLines = sizeOfText.height / label.font.pointSize;
You can compute the vertical height necessary to display the string using the sizeWithFont:constrainedToSize: method on NSString.
Given that size, you can resize the label to display the entire string.

Resources