developer
developer

Reputation: 658

UILABEL line break in collectin view IOS

I'm working with cell prototypes of collectionview in storyboard. In Collection View, I've set a UIIMAGE and UILABEL in a cell and calling programatically to load desired images with the help of viewWithTag to change the image and label as desired.

My Problem is, I'm unable to set my label such that cannot create a line break in words. The wraping of words are unable to give the label a line break in order to wrap a word and is giving the label in a single line.

I've tried to give linebreak from storyboard and also programatically but I'm unable to wrap my label and am unable to give a line break after a word.

My Code:

UILabel *label = (UILabel *)[cell viewWithTag:200];
label.text = [NSString stringWithFormat:@"%@",name];

My Try:

label.numberOfLines = 0; // also tried 2 for next line
label.minimumScaleFactor = 0.5;
label.lineBreakMode = UILineBreakModeWordWrap;
label.lineBreakMode = NSLineBreakByWordWrapping;

Upvotes: 1

Views: 904

Answers (1)

siejkowski
siejkowski

Reputation: 1681

The most possible reason for the lack of word wrapping is that UILabel doesn't know that it should not exceed given width.

There are two ways to express that:

  • setting property preferredMaxLayoutWidth on UILabel with value that indicates what width should not be exceeded

  • setting the correct autolayout constraints - you could do it either by specifying the width constraint or by specifying the offset/inset constraints - it really depends on what layout you're working on, since there is a huge number of possible combinations.

Best way to sanity-check it is to try preferredMaxLayoutWidth approach. If it works, than try to decide whether you want to leave this value set or you want layout to be more dynamic by finding the autolayout right constraints.

Upvotes: 3

Related Questions