Konstantinos Natsios
Konstantinos Natsios

Reputation: 2924

Break line on max width text UILabel swift 2

I'm currently trying to make an app that takes the usernames of some people and print them out in a custom cell, inside a label. But if a user has the name "John Goodwilkinson" It's a large name, so i'm trying to figure out how to make a

/n

Between the 2 words.

Does anyone has any idea? I've googled but couldnt find anything at all. Below you can see the screenshots with my code and the suggestions of the fellows :)

enter image description here

enter image description here

Also i have tried to put the code in the view controller that i call the custom cell like this

 cell.frndName.numberOfLines = 0
 cell.frndName.lineBreakMode = .ByWordWrapping

But it didn't work either

Upvotes: 0

Views: 1315

Answers (2)

Simpalm
Simpalm

Reputation: 1

You have to increase height of your UILabel and apply constant only for its width.

second thing you can add @“\n” by NSString *newUsername = [oldUsername stringByReplacingOccurrencesOfString:@" " withString:@“\n”];

it will replace all spaces between two words by @“\n” and next word will show in next line.

Upvotes: 0

Ismail
Ismail

Reputation: 2818

You can do that by setting the number of lines to the label to be 2 or 0 (if you wnat to be any number) and line breaks to be word wrap. Link this

enter image description here

Update after checking you UI. It ended up that removing the height/ width constraints and pinning the label from the bottom/ right the other controllers solved the problem.

Upvotes: 1

Related Questions