TechChain
TechChain

Reputation: 8944

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]];

Upvotes: 0

Views: 623

Answers (3)

Darshan Karekar
Darshan Karekar

Reputation: 681

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.

Upvotes: 13

Saheb Roy
Saheb Roy

Reputation: 5957

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

Upvotes: 1

rounak
rounak

Reputation: 9387

Ensure that you set the text of the label before calling size to fit.

Upvotes: 0

Related Questions