iOS sizeToFit not showing text - ios

I'm creating these labels for a Storyboard view. I have autolayout set to OFF and I'm setting the numberoflines to 0 but the text is only displayed if I comment out sizeToFit. Then, of course, the text gets cut off when its height is greater than 40.
- (void)viewDidLoad
{
[super viewDidLoad];
first = #"This text fits in the label";
second = #"This large text is too large for the label but because the words are normal sized, shows the more button correctly at the bottom right";
third = #"Doesn't show the more button correctly because WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW";
firstlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 57, 295, 40)];
secondlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 152, 295, 40)];
thirdlbl = [[UILabel alloc]initWithFrame:CGRectMake(13, 225, 295, 40)];
[firstlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[secondlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
[thirdlbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14]];
firstlbl.lineBreakMode = NSLineBreakByWordWrapping;
secondlbl.lineBreakMode = NSLineBreakByWordWrapping;
thirdlbl.lineBreakMode = NSLineBreakByWordWrapping;
firstlbl.numberOfLines = 0;
secondlbl.numberOfLines = 0;
thirdlbl.numberOfLines = 0;
[firstlbl sizeToFit];
[secondlbl sizeToFit];
[thirdlbl sizeToFit];
[firstlbl setText:first];
[secondlbl setText:second];
[thirdlbl setText:third];
[self.view addSubview:firstlbl];
[self.view addSubview:secondlbl];
[self.view addSubview:thirdlbl];
}

You're calling sizeToFit before you set the text, so it's sizing down to CGRectZero. Switch those calls and it'll work.
sizeToFit doesn't mean "automatically adjust your size to fit your content," it means "update your size right now to fit your content." Subsequent updates to the text won't change the frame.
If you want the former behavior, so that it automatically sizes itself to its content, autolayout is probably the easiest road to take.

Related

label disappear when use size to fit?

I am displaying a label on UI in iOS using code, not from storyboard. When the size of the label is large then it does display accurate it go outside the view so I decided to use sizeToFit in iOS. But when I apply this thing on label then it does appear on view. Please tell why my label does not appear in this case.
Here is the code for the label:
self.label_company = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 150, 100)];
[self.label_company setFont:[UIFont fontWithName:#"HelveticaNeue" size:12]];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 150, 100)];
[label1 setFont:[UIFont fontWithName:#"HelveticaNeue" size:12]];
label1.text=#"yourdata";
[self.view addSubview:label1];
[label1 sizeToFit];
Make sure you have some text in your label as well as add it to your view.
You currently have no text set to the label hence calling sizeToFit is making its width 0 as there is no size of the label in terms of width.
Set self.label_company.text = #"Some text" and then call sizeToFit
Ensure that you set the text of the label before calling size to fit.

Set distance between two dynamic Label

I want to fix a constant vertical distance between 2 UILabels.
When the first UILabel resizes, the UILabel below should dynamically adjust it's Y position.
This is what I have right now:
CGRect labelFrame = CGRectMake(20, 20, 200, 200);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setBackgroundColor:[UIColor orangeColor]];
NSString *labelText = #"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments...";
[myLabel setText:labelText];
[myLabel setNumberOfLines:0];
[myLabel setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel sizeToFit];
[self.view addSubview:myLabel];
CGRect labelsrame = CGRectMake(60, 200, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
[myLabel2 setBackgroundColor:[UIColor greenColor]];
NSString *label2Text = #"Floodgates. Floodgates. Keep 'em intact, gotta get home, gotta... gotta find... anywhere... mrph... just a few more blocks and I'll be there, and then this won't matter anymore... why didn't I just go before I left? I'm such an idiot. There's the hot dog vendor... past him, the bank... then another two blocks and I'll see apartments... almost home free, keep jogging, brisk... pace... hrm...";
[myLabel2 setText:label2Text];
[myLabel2 setNumberOfLines:0];
[myLabel2 setFont:[UIFont boldSystemFontOfSize:12]];
[myLabel2 sizeToFit];
[self.view addSubview:myLabel2];
Thanks
[myLabel sizeToFit] (in your case) changes the height parameter of myLabel while myLabel2 still starts at y=200 when it should start (as per your requirement) immediately after myLabel.
Hence, you need to make your frame settings dynamic too.
To fix the Y position issues for myLabel2, the following should be sufficient:
//...
CGRect labelsrame = CGRectMake(60, myLabel.frame.origin.y + myLabel.frame.size.height, 200, 200);
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:labelsrame];
//...
you have to create dynamic frame for both the label like bellow
UILabel *lblText1 = [[UILabel alloc]initWithFrame:CGRectMake(19, 250, 283, 20)];
lblText1.text = #"What ever your dynamic text";
CGSize maximumLabelSize = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize = [lblText1.text sizeWithFont:lblText1.font
constrainedToSize:maximumLabelSize
lineBreakMode:lblText1.lineBreakMode];
CGRect newFrame = lblText1.frame;
newFrame.size.height = expectedLabelSize.height;
lblText1.frame = newFrame;
// label 2
UILabel *lblText2 = [[UILabel alloc]initWithFrame:CGRectMake(19, 280, 283, 20)];
lblText2.text = #"What ever your dynamic text label 2";
CGSize maximumLabelSize1 = CGSizeMake(280,9999); // label with fix width 280
CGSize expectedLabelSize1 = [lblText2.text sizeWithFont:lblText2.font
constrainedToSize:maximumLabelSize1
lineBreakMode:lblText2.lineBreakMode];
CGRect newFrame1 = lblText2.frame;
newFrame1.size.height = expectedLabelSize1.height;
newFrame1.origin.y = lblText1.frame.origin.y + lblText1.frame.size.height + 10; // 10 for fix space between 2 label
lblText2.frame = newFrame1;

Adjust height according to number of lines

I am trying to adjust height of my view according to how many lines inserted text will take. Then [title numberOfLines] always returns 0 instead of the new number of lines. Is there a way to get current number of lines?
UILabel *title = [[ UILabel alloc] initWithFrame:CGRectMake(0, 80, 320, 50)];
[title setNumberOfLines:0];
float height = 32 * [title numberOfLines];
[self setFrame:CGRectMake(0, yCoord, 320, height)];
I think your problem come from the fact you did n't set any text and font to your UILabel.
What you could use is [UIView sizeToFit]
Here is a short sample code:
UILabel *title = [[ UILabel alloc] initWithFrame:CGRectMake(0, 80, 320, 50)];
[title setText:#"An awesome title very long or something really important like a Lorem Ipsum"]; // a text to base the needed numbers of line on
[title setFont:[UIFont systemFontOfSize:18]]; // don't forget the font
[title setNumberOfLines:0];
[title sizeToFit]; // now the label will be resize to display all lines
[title setFrame:CGRectMake(0, 80, 320, title.frame.size.height)]; // reset the width in case the sizeToFit method resized it wrong.
Ended up using
[title sizeToFit];
int numLines = (int)(title.frame.size.height/title.font.leading);
From another answer

How add Multiple Lines in Same UILabel

Any way to have multiple lines of text in UILabel ?
I dont wish to more than 1 label in the view.
How to add multiple lines in a single UILabel??
Yes there is a way. Just you need to add two property of UILabel i.e.
NumberOfLines=0 It'll allow you to add multiple lines in a UILabel
LineBreakMode = NSLineBreakByWordWrapping It'll allow you to break your sentence by word. You can also change it according to your requirement.
[YourLabel setNumberOfLines:0];
[YourLabel setLineBreakMode:NSLineBreakByWordWrapping];
You can also set this two property form your interface builder
here is a sample code
UILabel *pHolder1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 245, 45)];
pHolder1.backgroundColor = [UIColor clearColor];
pHolder1.font = [UIFont systemFontOfSize:14.0f];
pHolder1.numberOfLines =0;
pHolder1.lineBreakMode = NSLineBreakByWordWrapping;
pHolder1.textAlignment = NSTextAlignmentCenter;
Dynamically calculate the height of UILabel
please refer the below post
Adjust UILabel height depending on the text
Here is sample code:
UILabel *lblUsername=[[UILabel alloc] init];
StoryTextSize = [storytext sizeWithFont:[UIFont fontWithName:#"Georgia" size:13.0f] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
lblUsername.frame=CGRectMake(20, 5, [[UIScreen mainScreen] bounds].size.width-40, StoryTextSize.height);
lblUsername.textColor=[UIColor blackColor];
lblUsername.text=[NSString stringWithFormat:#"%#",[[tblRecords objectAtIndex:indexPath.row] valueForKey:#"username"]];
lblStoryText.numberOfLines=nooflines;
lblStoryText.backgroundColor=[UIColor clearColor];
[self.view addSubview:lblStoryText];
make sure that your label height should be more so total no of lines become visible.

How to have a page with long text that is wrapped automatically based on the phone screen

I am trying to make a book application. I am stucked with the text that can show long string. Basically, I tried to use UI Label, but it is only showing one line and the remaining of the text is cut.
Any idea what kind of UI that I should use to make a book? Thanks.
Set the numberOfLines for the UILabel to 0, to let it use as many lines as it needs.
You have to use UITextView for such purpose check
UITextView *aboutStable = [[UITextView alloc] init];
[aboutStable setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[aboutStable setText:#"Write your long text here............iphonemaclover is great"];
[aboutStable setTextColor:[UIColor grayColor]];
[aboutStable setBackgroundColor:[UIColor clearColor]];
[aboutStable setTextAlignment:UITextAlignmentCenter];
[aboutStable setFrame:CGRectMake(0, 302, 320, 79)];
[self addSubview:aboutStable];
And make it not editable so set
[aboutStable setUserInteractionEnabled:NO]
should do it
and if you need scrolling:
aboutStable.editable = NO;
or if you text is still more,then you can set scrollview in it too...
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = #"your long text here";
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];//add scrollview using Xib and set property of it.
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
or you can check more detail here in this sample code
EDIT FOR DIFFERENT RETINA DISPLAY
CGRect screenRect = [[UIScreen mainScreen] bounds];
if (screenRect.size.height == 568.0f)
{
//condition for iphone 5 screen
}
else
{
//condition for retina display 3.5
}

Resources