UILabel Font and height in IOS - 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.

Related

Unable to show all content of UILabel

I have a UILabel which is by default showing only 3 lines of text. I want to display all text content in some cases. I have tried everything I found, but nothing works. Here is my latest try:
CGRect labelSize = [contents.contentLanguage.Description boundingRectWithSize:CGSizeMake(self.view.frame.size.width * 0.97, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
cell.descLbl.frame = CGRectMake(labelSize.origin.x,
labelSize.origin.y,
ceil(labelSize.size.width),
ceil(labelSize.size.height));
[cell.descLbl setNumberOfLines:0];
[cell.descLbl setLineBreakMode:NSLineBreakByWordWrapping];
[cell.descLbl sizeToFit];
Maybe you should look at maybe using the UITextView instead? It's much better suited for multiline text.
https://developer.apple.com/documentation/uikit/uitextview
Ok, so I was able to resolve this issue by changing the height of the UILabel from the .xib file to >=0
Please use UiTextView in place of UILabel
textviewobj.text=#"";
[textviewobj sizeToFit];
You have two ways (choices) to do it:
Use UITextView without editing permission
Use UILabel with numberOfLines = 0
Use UITextView without editing permission
If you've fixed space (limited area to display your content i.e. fixed height) to diplay your content and you can't work with scrollable content then you should use UITextView without editing permission.
Use UILabel with numberOfLines = 0
If you've flexible space (no limited space/area to display a your content i.e. fixed height) to display your content, then you should use UILabel with number of line = 0.
Do not assign height value (constraint) if you want a flexible UILabel with height, that fits according to size of your content.
Or Assign minimum height value (constraint like >= 30) if you want to have/set a minimum area for content, whether there is no content to be display.

Vertically Center UILabel Size Resized Font

I have a UIlabel with constraints set. Constraint to both left and right sides and vertical center were set to the label. The parent view width is dynamic, means can be wider or not but the height is static. The label will be set with some text that are long or short.
I also set the minimum font size so that the font would not wrap and set it to one line only. Since I set the minimum font size, the text height auto adjusts to fit the text but the problem the text is not centered vertically instead it sits at the bottom of the UIlabel.
I tried to resize the height of the UIlabel but I can't get it to resize with the right size based on the new font size and when to set the new size. I tried the solutions here by getting the font size and setting the height dynamically instead it caused unexpected results. I am running it on iOS 6.
I figured it out myself. Below is a sample code
+(void)autoFontResizeLabel:(UILabel*)label inView:(UIView*)containerView{
NSString *text = label.text;
//determine the font size
CGFloat actualFontSize;
[text sizeWithFont:label.font
minFontSize:18
actualFontSize:&actualFontSize
forWidth:label.bounds.size.width
lineBreakMode:label.lineBreakMode];
//reset the label content and frame
[label setText:#""];
[label setText:text];
//turn off auto adjust to allow new text with correct font
label.adjustsFontSizeToFitWidth = NO;
[label setFont:[UIFont fontWithName:#"HelveticaNeue" size:actualFontSize]];
//optional
[label sizeToFit];
CGSize barSize = containerView.frame.size;
CGFloat titleY = fabsf(barSize.height - label.frame.size.height)/2;
[label setFrame:CGRectMake(label.frame.origin.x, titleY, label.frame.size.width, label.frame.size.height)];
}

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

Adjust width of UILabel based on its contents

I had a look on SO before asking this question. All the questions are about adjusting the height of a UILabel and not its width. I tried alternative methods but it did not work such as [label sizeToFit];. Is it possible to adjust the width of a label based on its text? I create my label in a UITableViewCell in story board. I want to be able to adjust its width based on the text that it is assigned. I dont want to resize the font size.
I set the text of the label in CellForRowAtIndexPath.
Examples would be great please.
Thanks in advance :)
Update 1: I have a custom cell that I am making in Storyboard so not programmatically. I set the contents of each cell in CellForRowAtIndexPath, for example, myLabel.text = recipe.name. The name label is quite small, however I would like to extend its width based on the length of the text, not truncate the tail or shrink the size of the text.
Update2: I have a lot of other UIElements in the cell. So I have a label in the top left, top right, bottom left, bottom right, and a picture in the middle, there default is 120 because they have a background color. I set it small the there is not a huge amount of empty space in the label.
Get the size of the string:
//Replace FLT_MAX with the maximum height/width you want the label to be, if no maximum leave as FLT_MAX.
CGSize stringSize = [YOUR_STRING sizeWithFont:YOUR_FONT constrainedToSize:CGSizeMake(FLT_MAX, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
Then size your label:
[YOUR_LABEL setFrame:CGRectMake(0, 0, stringSize.width, stringSize.height)];
In iOS 7 sizeWithFont is deprecated, use boundingRectWithSize instead:
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:YOUR_LABELS_FONT
forKey: NSFontAttributeName];
CGSize stringSize = [text boundingRectWithSize:CGSizeMake(FLT_MAX, FLT_MAX)
options:NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin
attributes:stringAttributes context:nil].size;
CGSize maxSize = CGSizeMake(250, CGFLOAT_MAX); //250 is max desired width
CGSize textSize = [Label.text sizeWithFont:Label.font constrainedToSize:maxSize];
Label.frame = CGRectMake(0, 0, textSize.width, 15); // desired Bounds
Using above code you can get frame size for perticular text, & then resize label accordingly
You don't need to set explicit width for UILabel. UILabel provides intrinsic content size when used with autolayout. If you add constraints that provide the label's x,y position(Top Space constraint + Leading Space constraint), autolayout will be able to determine its width height based on the content.

Autosize UILabel

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.

Resources