UILabel number of lines affecting the bounds size - ios

I am having this peculiar behavior with UILabel. Any numberOfLines works ok, except 1. If I set the number of lines to 1 it ignores the width which I set later.
I don't understand why 1 line screws it up...
here is my code
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = #"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.frame = CGRectMake(20, 20, 100, 0);
CGRect rect = label.frame;
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
rect.size.width = 100;
label.frame = rect;
[label sizeToFit];

Use this code:
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = #"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.frame = CGRectMake(20, 20, 100, 0);
label.numberOfLines = 3;
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
[label sizeToFit];
CGRect rect = label.frame;
rect.size.width = 100;
label.frame = rect;
With numberOfLines = 3:
With numberOfLines = 1:

If you wanna use numberOfLines = 1 in that case your text will be in one line.So please use numberOfLines = 0;
label.numberOfLines = 0;
And there is no need to again define label frame so please remove these statement.
CGRect rect = label.frame;
rect.size.width = 100;
label.frame = rect;
Use this code this is perfect..
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = #"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.frame = CGRectMake(20, 20, 100, 0);
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
[label sizeToFit];

Yes, it doesn't work when numberOfLines=1
I have to add this line at the end to make it work for all cases..
label.width = min(label.width, 100)

Use this as :
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor greenColor];
label.text = #"here is my label with lots of text to fill, here is my label with lots of text to fill";
label.numberOfLines = 0;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(100 , 9999) lineBreakMode:label.lineBreakMode];
float lHeight = labelSize.height;
label.frame = CGRectMake(20, 20, 100, lHeight);
label.lineBreakMode = NSLineBreakByTruncatingTail;
[self.view addSubview:label];
EDIT : - (void)sizeToFit
Description :
Resizes and moves the receiver view so it just encloses its subviews.
Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.
// [label sizeToFit];
Hope it helps you.

Related

UIFont for Label not cooperating with size settings

I want to make a game title but I can't seem to make the font larger. Here's what I have. I opted to not use CGRectMake initializer setting because I wanted to just set the x and y positions, not the size of the label (which I presumed would've been dictated by the size: portion of the UIFont method below). The 100.0 is no different from size 40.0.
UILabel *title = [[UILabel alloc] init];
CGRect frame = title.frame;
frame.origin.y = 30;
frame.origin.x = 100;
title.frame= frame;
title.text = #"TWINSTONES";
title.backgroundColor = [UIColor clearColor];
title.font = [UIFont fontWithName:#"TWINSTONES" size:100.0];
[title setTextColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
title.numberOfLines = 0;
[title sizeToFit];
[self.view addSubview:title];

UILabel sizeThatFits too wide for 1-line label

I have a label that I'm creating and displaying programmatically. It can be 1 or more lines. I want to the label to be truncated at the end if it's too long. When the label is > 1 line long the following code works fine. Create a blank project and drop this into viewDidLoad to play along at home. Any iOS or tvOS project should do.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.backgroundColor = [UIColor blueColor];
[self.view addSubview:label];
NSDictionary *attributes = #{NSFontAttributeName:[UIFont systemFontOfSize:26.0]};
label.attributedText = [[NSAttributedString alloc] initWithString:#"The rain in Spain falls mainly on the plain." attributes:attributes];
CGSize maxLabelSize = CGSizeMake(200, CGFLOAT_MAX);
CGSize requiredSize = [label sizeThatFits:maxLabelSize];
NSLog(#"requiredSize: %#", NSStringFromCGSize(requiredSize));
label.frame = CGRectMake(50.0, 50.0, requiredSize.width, requiredSize.height);
However, if I change numberOfLines to 1 then sizeThatFits returns a size with a width wide enough to fit the entire string even though it's bigger than the width of maxLabelSize.
I can work around this by checking to see if requiredSize.width is greater than maxLabelSize.width, and adjusting appropriately, but I'd like to know why sizeThatFits behaves differently with a 1-line label than with a multi-line label. I would expect a size no greater than 200 with the height the same as the attributed string's line height.
I have no idea why sizeThatFits doesn't work, but another method textRectForBounds:limitedToNumberOfLines: does the trick. Something like
label.numberOfLines = 0;
CGSize requiredSize = [label textRectForBounds:CGRectMake(0, 0, 200, CGFLOAT_MAX) limitedToNumberOfLines:1].size;
UILabel * commlbl;
commlbl=[[UILabel alloc]initWithFrame:CGRectMake(10, commlbl1.bounds.size.height+50, commscroll.bounds.size.width-25, commscroll.bounds.size.height+70)];
[commlbl setFont:[UIFont fontWithName:#"OpenSans-Regular" size:16]];
[commlbl setTextColor:[UIColor whiteColor]];
[commlbl setTextAlignment:NSTextAlignmentCenter];
commlbl.lineBreakMode = NSLineBreakByWordWrapping;
commlbl.numberOfLines = 0;
commlbl.text = [USER_DFT GetUserDefault:#"msgString"];
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);
CGSize expectedLabelSize = [commlbl.text sizeWithFont:commlbl.font constrainedToSize:maximumLabelSize lineBreakMode:commlbl.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = commlbl.frame;
newFrame.size.height = expectedLabelSize.height;
commlbl.frame = newFrame;

Trouble positioning custom navigationItem titleView

I have a custom view (titleView) that I've created for a webView on my iOS app. The titleView has two labels, a titleLabel and a subTitleLabel. If the titleLabel is too wide for the titleView, I truncate the text and have it fill the whole frame. But the problem happens whenever the titleLabel is smaller that the titleView. I get inconsistent results when I try to calculate the position of the titleLabel's frame. I just assumed I would take the difference between the width of the titelView.frame.size.width and the titleLabel.frame.size.widthand divide that by 2, but it doesn't work. I'm probably missing something stupid, but I just can't see it. One thing to note is that the subTitleLabel's seem to be positioned fairly well, and though it's not perfect, it's better than the titleLabel's.
Here are some images (I added borders to the titleView and it's subViews to help show positioning) that show positioning with various length title strings:
Code:
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width - 88, 34)];
titleView.clipsToBounds = YES;
UIFont *titleFont = [UIFont systemFontOfSize:16 weight:UIFontWeightThin];
UIFont *speakerFont = [UIFont systemFontOfSize:10 weight:UIFontWeightLight];
CGSize speakerSize = [[self.speech speakerFullNameAndDate] sizeWithAttributes:#{ NSFontAttributeName : speakerFont }];
CGSize titleSize = [self.speech.title sizeWithAttributes:#{ NSFontAttributeName : titleFont }];
UILabel *titleLabel = [[UILabel alloc] init];
UILabel *subTitleLabel = [[UILabel alloc] init];
[titleView addSubview: titleLabel];
[titleView addSubview:subTitleLabel];
CGFloat titleDifference = (titleView.frame.size.width - titleLabel.frame.size.width) / 2;
titleLabel.text = self.speech.title;
titleLabel.font = titleFont;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
// titleLabel is bigger than the titleView's frame
if (titleSize.width > titleView.frame.size.width) {
titleLabel.frame = CGRectMake(0, 0, titleView.frame.size.width - 20, 18);
} else {
// titleDifference / 3 seems to be the best number for the frame's x coordinate
titleLabel.frame = CGRectMake(titleDifference / 3, 0, titleSize.width, 18);
[titleLabel sizeToFit];
}
subTitleLabel.text = [self.speech speakerFullNameAndDate];
subTitleLabel.font = speakerFont;
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.textColor = [UIColor whiteColor];
subTitleLabel.backgroundColor = [UIColor clearColor];
// Again, ((titleView.frame.size.width - speakerSize.width) / 3) seems to work best, though it's far from perfect
subTitleLabel.frame = CGRectMake(((titleView.frame.size.width - speakerSize.width) / 3), 20, speakerSize.width, 12);
[subTitleLabel sizeToFit];
self.navigationItem.titleView = titleView;
You are calculating your title label origin wrong remember that any view origin is the top left corner, so your title label origin should be something like
CGFloat originX = titelView.frame.size.width/2 - titleLabel.frame.size.width/2
what you are doing is assuming a view origin is in the center of the view

boundingRectWithSize adds extra bottom padding to UILabel

UILabel is created like this:
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 10, 268, 44)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor yellowColor];
label.numberOfLines = 2;
label.lineBreakMode = NSLineBreakByTruncatingTail;
Then to update the label I do this:
label.text = someText;
CGRect frame = label.frame;
NSDictionary *attributes = #{NSFontAttributeName: label.font};
CGRect rect = [someText boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
frame.size.height = rect.size.height;
label.frame = frame;
This works fine with the label only has one line of text, but I have a maximum of two lines. When the 2nd line gets truncated, extra bottom padding gets added to the label, which makes it impossible to position the label below it (the red label in the images):
Why does this happen? If it's a bug, is there a workaround?
Try something like this:
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:text attributes:attributes];
UILabel *anotherLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myLabel.frame.size.width, MAXFLOAT)];
[anotherLabel setAttributedText:attrString];
[anotherLabel setNumberOfLines:2];
[anotherLabel setText:NSTextAlignmentLeft];
[anotherLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[anotherLabel sizeToFit];
[myLabel setAttributedText:attrString];
[myLabel setFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, [anotherLabel frame].size.height)];

Programmatically Creating UILabel

I know this should be very simple, but I've been really struggling with creating a UILabel programmatically and getting it to behave the way I would like.
All I want is to be able to create a label, set the maximum attributes as far as height, width and font size, and then have the text get smaller AND/OR just truncate the text to accommodate long strings of text.
Let's say that I want my label to have text that has a maximum width of 380, a maximum height of 20 and a maximum font size of 12.
So here is what I have attempted to do to create such a label:
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 0, 0)];
fromLabel.text = [self fromSender];
fromLabel.font = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = YES;
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.size = [fromLabel.text sizeWithFont:fromLabel.font constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];
fromLabel.minimumScaleFactor = MIN_SCALE_FACTOR;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];
Ok, so the label appears, but the text is larger than 12 and the height always comes out to be 21!? Even if I change the height and text size values to extreme sizes, this code creates a label of some fixed size that can't be adjusted. The only thing I can seem to make smaller is the width.
I must be over looking something basic, but I really can't figure out how to get my desired result, nor do I understand why I'm getting the behavior that I am getting.
Does the following work ?
UIFont * customFont = [UIFont fontWithName:ProximaNovaSemibold size:12]; //custom font
NSString * text = [self fromSender];
CGSize labelSize = [text sizeWithFont:customFont constrainedToSize:CGSizeMake(380, 20) lineBreakMode:NSLineBreakByTruncatingTail];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
fromLabel.text = text;
fromLabel.font = customFont;
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[collapsedViewContainer addSubview:fromLabel];
edit : I believe you may encounter a problem using both adjustsFontSizeToFitWidth and minimumScaleFactor. The former states that you also needs to set a minimumFontWidth (otherwhise it may shrink to something quite unreadable according to my test), but this is deprecated and replaced by the later.
edit 2 : Nevermind, outdated documentation. adjustsFontSizeToFitWidth needs minimumScaleFactor, just be sure no to pass it 0 as a minimumScaleFactor (integer division, 10/12 return 0).
Small change on the baselineAdjustment value too.
UILabel *lbl1 = [[UILabel alloc] init];
/*important--------- */lbl1.textColor = [UIColor blackColor];
[lbl1 setFrame:position];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=NO;
lbl1.text= #"TEST";
[self.view addSubview:lbl1];
here is how to create UILabel Programmatically..
1) Write this in .h file of your project.
UILabel *label;
2) Write this in .m file of your project.
label=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];//Set frame of label in your viewcontroller.
[label setBackgroundColor:[UIColor lightGrayColor]];//Set background color of label.
[label setText:#"Label"];//Set text in label.
[label setTextColor:[UIColor blackColor]];//Set text color in label.
[label setTextAlignment:NSTextAlignmentCenter];//Set text alignment in label.
[label setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines];//Set line adjustment.
[label setLineBreakMode:NSLineBreakByCharWrapping];//Set linebreaking mode..
[label setNumberOfLines:1];//Set number of lines in label.
[label.layer setCornerRadius:25.0];//Set corner radius of label to change the shape.
[label.layer setBorderWidth:2.0f];//Set border width of label.
[label setClipsToBounds:YES];//Set its to YES for Corner radius to work.
[label.layer setBorderColor:[UIColor blackColor].CGColor];//Set Border color.
[self.view addSubview:label];//Add it to the view of your choice.
Swift 3:
let label = UILabel(frame: CGRect(x:0,y: 0,width: 250,height: 50))
label.textAlignment = .center
label.textColor = .white
label.font = UIFont(name: "Avenir-Light", size: 15.0)
label.text = "This is a Label"
self.view.addSubview(label)
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 300, 50)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = #"Your Text";
[self.view addSubview:label];
In Swift -
var label:UILabel = UILabel(frame: CGRectMake(0, 0, 70, 20))
label.center = CGPointMake(50, 70)
label.textAlignment = NSTextAlignment.Center
label.text = "message"
label.textColor = UIColor.blackColor()
self.view.addSubview(label)
For swift
var label = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 50))
label.textAlignment = .left
label.text = "This is a Label"
self.view.addSubview(label)
UILabel *mycoollabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 70, 50, 50)];
mycoollabel.text=#"I am cool";
// for multiple lines,if text lenght is long use next line
mycoollabel.numberOfLines=0;
[self.View addSubView:mycoollabel];

Resources