Autosize UILabel - ios

Is there a way to auto size a UILabel? given size 40 x 40 the text font size would adjust based on the number of characters.

You can use the adjustFontSizeToFitWidth property. So something like this.
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setAdjustsFontSizeToFitWidth:YES];
In Interface Builder there is a check box on the Label Attributes screen to allow you to adjust the font size to fit the label as well.

uhm, did you check out the UILabel API http://developer.apple.com/iphone/library/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html there's a neat property called adjustsFontSizeToFitWidth

With autolayout design concept, do not set height constraints for UILabel and set no. of lines as 0.
Autolayout automatically take care of dynamic height of label according to text of label. If label has single line text then it will occupy single line space only. And if label has more than one line then it will resize label according to text size and number of lines required to display text.
Set number of lines zero for dynamic text information, it will be useful when your text are varying.
Programatically (Swift 4)
var label = UILabel()
let stringValue = "iOS\nmultiline\nlabel\nin\nInterface\nbuilder"
label.text = stringValue
label.numberOfLines = 0 // Set 0, if number of lines not specified.
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame
Using Inetrface Builder
Note: It is not required to set Minimum Font Scale, but nice to have a minimum scale factor to fit text into label frame.
Ref: UILabel - numberOfLines

Its better to use Intrinsic content and compression resistance priorities to adjust the size of label w.r.t content.

Related

UITextView autoresize to adjust on a single line iOS Objective -C

I have a label called ratingstext and I want its contents to fit into a single line of text by means of adjusting height in case of multiple lines. How do I achieve that in iOS Objective -C.
My label:
as you can see the label shows 200/20 and is called ratingstext label. Actually the value is 200/200 but since I have used
self.ratingsText.textContainer.maximumNumberOfLines = 1;
The last 0 value gets chopped off due to fixed label width. How do I auto adjust width or height to fit in the content in a single line?
My code is:
self.ratingsText = [[UITextView alloc]initWithFrame:self.frame];
self.ratingsText.editable = NO;
self.ratingsText.text = #"000/00";
self.ratingsText.textContainer.maximumNumberOfLines = 1;
self.ratingsText.font = [UIFont systemFontOfSize:self.ratingSize weight: UIFontWeightRegular];
[self.ratingsText sizeToFit];
self.ratingsText.backgroundColor = UIColor.clearColor;
[self addSubview:self.ratingsText];
If you are using a storyboard autoLayout will expand the frame to match the size of the text input. In the Attributes Inspector make the width textLabel "Relation" to "Less Than or Equal". I gave the text label in the storyboard 200/20 and in code set it to 200/200. Auto Layout took care of the rest.
Use the below properties:
adjustsFontSizeToFitWidth
contentScaleFactor
minimumFontSize

How can I set a multiline UILabel inside UITableCell on Swift 3?

I am trying to set a multiline UILabel, inside an UITableCell.
That UILabel has a proportional width taking as reference the screen of the device (with a multiplier). The text inside of it can change so I need to fit on the UILabel being able to multiline when the width of the text is higher than the space of the UILabel width.
I tried using:
myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
myLabel.sizeToFit()
but it is always displayed on one line truncating the text that overflows the UILabel width.
How can I make my UILabel to be multiline?
EDIT: I cannot put breaklines to my text because I retrieve my texts from my database.
Thanks in advance!
These two lines are necessary for calculating the cell height automatically. Put these in viewDidLoad.
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
If you want a label to switch from one line to multiple lines, you either have to specify line breaks in the text of the string with \n or newline char, or you can set constraints for the width of the label, or you can set leading and trailing constraints for the label. If one of those methods is complete, the label will go to as many lines as it takes to hit the max number of lines or infinite if set to 0.
Finally I was able to make my multiline UILabel adding this code:
self.tableView.estimatedRowHeight = 50.0
and reducing the multiplier value. It seems that the text could not fit on the height of the UITableCell.

UILabel Font and height in IOS

I see that UILabel in IOS has height property along with font . I also see that if I give the height and font same value, for some characters the text gets cut in UILabel. How are these two different? While doing the UI design if height is more than the font size, there would be some extra white space which is what I want to avoid and hence wanted to know what exactly is the difference between the two.
You can use sizeThatFits: to determine correct height for UILabel
UILabel *label;
label.text = #"Some text";
CGRect labelFrame = label.frame;
labelFrame.size.height = [label sizeThatFits:CGSizeMake(labelFrame.size.width), MAXFLOAT].height;
label.frame = labelFrame;
The text size is the height from the font's baseline to its cap line, which doesn't take into consideration descenders (like on the letters 'g' and 'y') or ascenders (like 'f', in some fonts). In general, you don't have to (or even need to) set a label's height; it determines the best height based on its font size.
See https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html for details about how font sizes work.

How to limit max width of UILabel?

There are a UILabel in the center of the UIView, named nicknameLabel, and will append a gender image after the nicknameLabel.
But when the nicknameLabel has a lot of text, it will beyond the bounds of the UIView.
So, how to limit max width of UILabel?
BTW: I using the storyboard. Thanks.
Give the label a <= constraint like this:
use following code and set your width in boundingRectWithSize value.
CGSize itemTextSize = [#"Your Text" boundingRectWithSize:CGSizeMake(100, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName : [UIFont fontWithName:#"Helvetica Neue" size:12.5]} context:nil].size;
If you are using constraints then remove all constraints from you label.
Then again set frames of your label and if you want to make multiline label, then simply set the number of lines from 1 to 0 and increase the height of your label.
OR
If you want to fit your text within the label.You can simply enable Autoshrink property.

Adjust the font of UILabel so that the text fits within the label's bounds

My view controller has a UILabel. It displays a dollar amount to the user. I have a default size that I like the dollar amount to be displayed as, shown below
When the amount is too large, some text is replaced with "..."
I want to adjust the text of the UILabel (make it smaller) such that the point size of the text is just enough to not cause the dots to appear. Is there some quick math I can do to accomplish this?
label.adjustsFontSizeToFitWidth = YES;
label.numberOfLines = 1;
No need for you to make the math, the system-provided API can do it for you. In Interface Builder, set the label's autoshrink to minimum scale factor of 0.7 (or whatever value fits your need). Text will now shrink as necessary.
Use this:
UILabel * label = // your label
// update from #rmaddy and #z s
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
This means that you'll allow your text to shrink to up to half its size before truncating. Adjust accordingly.
If you're working in a pre iOS 7 environment, you can also use:
UILabel * label;
label.minimumFontSize = 14;
To set a specific minimum font size; however, this has been deprecated since iOS 7

Resources