4

As mentioned many times before, neither label.sizeToFit() nor label.numberOfLines=0 work for me. All the time I end up with the same height of label with text ended with "…".

I really want to keep my label width constant with adjustable height, but any solution I found around the forum wasn't working for me. I know it would be better to ask it as a comment to existing question, but unfortunately I'm not able to do that due to my point's account.

Do you know any other ways to achieve adjustable height of UILabel, that actually work?

Here is what I have:

    cell.restaurantName.setTranslatesAutoresizingMaskIntoConstraints(false)
    cell.restaurantName.numberOfLines = 0
    cell.restaurantName.sizeThatFits(CGSizeMake(CGFloat(172.0),    CGFloat(MAXFLOAT)))

enter image description here then I tried exactly the same as posted below

cell.restaurantName.numberOfLines = 0
cell.restaurantName.sizeToFit()

with constraint to width

enter image description here

and again I see text with three dots

Thanks in advance

theDC
  • 6,364
  • 10
  • 56
  • 98
  • I believe there's too many solutions to put into one good detailed answer, here's the [most simple](http://stackoverflow.com/a/13032251/792677) that I found in seconds. – A-Live Jan 21 '15 at 19:49

1 Answers1

4

The simplest solution is to create the label, add your label to the view then set your constraints. As long as the horizontal constraints are set up in such a way the label knows what its maximum width can be then you can call the sizeThatFits for the label so it will size correctly downloads. Below is some Objective C code that will do exactly what you are wanting.

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.numberOfLines = 0;
label.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
[self.view addSubview:label];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(10)-[label(300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[label sizeThatFits:CGSizeMake(300.0, MAXFLOAT)];

The label will be a maximum of 300 in width and will take the rest of the height available based on what the text is in the UILabel

And since it is a swift question here is the same answer in swift

var label: UILabel = UILabel(frame: CGRectZero)
label.setTranslatesAutoresizingMaskIntoConstraints(false)
label.numberOfLines = 0
label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
view.addSubview(label)

let views = Dictionary(dictionaryLiteral: ("label", label))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-(10)-[label(300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(10)-[label]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
label.sizeThatFits(CGSizeMake(CGFloat(300.0), CGFloat(MAXFLOAT)))

With the storyboard you can just do the following:

Storyboard-label-constraints

 @interface ViewController ()

@property (nonatomic, weak) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.label.numberOfLines = 0;
    self.label.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    [self.label sizeToFit];
}

@end
darren102
  • 2,830
  • 1
  • 22
  • 24
  • Is it the same with label which is iboutlet? Does it Make any difference? – theDC Jan 21 '15 at 21:23
  • If your using an IBOutlet you would set up your constraints for width and positioning using the Interface Builder. This code above showed how to do it in the code base but in Interface Builder you can just drag a label and set its x, and y constraints then width constraint and it will work the same way. Making sure to set the number of lines to 0 also. – darren102 Jan 21 '15 at 21:25
  • Thanks, i'll give a try and let know if i succeed – theDC Jan 21 '15 at 21:26
  • Still the same, nothing has changed… I'll edit the question with code and screenshot, please have a look if you don't mind – theDC Jan 21 '15 at 22:38
  • could you take a look @darren102 – theDC Jan 21 '15 at 22:52
  • in your screenshot it says the view does not have any constraints. You need to add constraints to it in the Storyboard / IB Builder. You need a and 'X' and 'Y' then you need the width. That is the three that are in the image i show. Once they are there it should work without issue. – darren102 Jan 21 '15 at 23:12
  • Yeah, finally! It was all about this width constraint. Once I add it xcode screams about ambuguity of height, is there any way to eliminate this warning? But I'm so happy it works, thanks Darren! – theDC Jan 21 '15 at 23:24