JP King
JP King

Reputation: 13

xcode UILabel Text fits in storyboard but not on device

I've created a custom cell in tableview and have added some labels to it. On setting constant text in the storyboard for each label, the text fully fits inside the labels properly (the size of each view is set to iPhone 5.5 inch). But as soon as I run the application on iPhone 6 (5.5 inch), the table cell generated does not fit the text inside the label properly and shows incomplete text ending with ellipsis. If the text fits properly in the label in Storyboard, why isn't it fitting properly in the device even if the screen sizes for both the cases is same? How to fix this problem?

EDIT: I've also added the lines in my code to automatically fit the text in the labels:

label.numberOfLines = 1
label.adjustsFontSizeToFitWidth = true
label.sizeToFit()

But it still doesn't work.

Any help would be appreciated. Thanks in Advance.

Upvotes: 0

Views: 1248

Answers (4)

NewStackUser
NewStackUser

Reputation: 657

For me the below code works with every label that I use. This resizes the label according to the content description.

label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.numberOfLines = 0
label.sizeToFit()

I hope this helps you in your custom cell too.

Upvotes: 1

MSU_Bulldog
MSU_Bulldog

Reputation: 3519

You can do this in Interface Builder. Click your label and on the right inspector tab find Autoshrink. Change that to Minimum Font Size and select what the minimum size you would like your font to be (I would suggest 12). I know you said that you have tried using Autoshrink, but this works. No need to put label.sizeToFit()

Upvotes: 0

Bhanu
Bhanu

Reputation: 1249

Replace below code

label.numberOfLines = 0
label.adjustsFontSizeToFitWidth = true;
label.sizeToFit()

This setup you UILabel best

Upvotes: 0

Prabhu.Somasundaram
Prabhu.Somasundaram

Reputation: 1390

call the below lines once you set the values for your label

self.yourLabel.adjustsFontSizeToFitWidth = true;
self.yourLabel.sizeToFit()

Upvotes: 0

Related Questions