3

I'm trying to create a UILabel programmatically that fits it's content. The best I could do was to use the NSString's method sizeWithAtributes to get it's content's required width and then apply a constraint that fixes the width to that value.

Is this the way it is supposed to be done using autolayout?

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
Mppl
  • 941
  • 10
  • 18

4 Answers4

3

At First read the best answer for any question about UILabel

And also by using following code you can create dynamic size of UILabel

UILabel *myLabelName = [[UILabel alloc] init];
myLabelName.backgroundColor = [UIColor redColor];
[myLabelName  setFont: [UIFont fontWithName:@"OpenSans" size:13]]; // set font and it's size as per your requirement.
myLabelName.textAlignment = NSTextAlignmentLeft;
myLabelName.frame = CGRectMake(lblComplainTitle.frame.origin.x + lblComplainTitle.frame.size.width + 7, lblComplainTitle.frame.origin.y +2, 160, 20);
myLabelName.text = @"This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text.";
myLabelName.textColor = [UIColor blackColor];
[self setDynamicHeightOfLabel:myLabelName withLblWidth:160 andFontSize:13]; // here in this function you need to pass object of label with width (as you wabt) and font size
[self.view addSubview:myLabelName];

Code of function, name is setDynamicHeightOfLabel:withLblWidth:andFontSize

-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
    CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
    CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
    CGRect lblFrame = myLabel.frame;
    lblFrame.size.height = expecteingmyLabelSize.height;
    myLabel.frame = lblFrame;
    int addressLine = myLabel.frame.size.height/fontSize;
    myLabel.numberOfLines = addressLine;
}

I create function because you can create any label with dynamic size without use of repeated code.

Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
3

if you need some with aoutolayouts, i try comment:

    UILabel *testLabel = [UILabel new];
    [testLabel setFont:[UIFont systemFontOfSize:16]];
    [testLabel setText:@"Test text for size context"];
    [testLabel setTextAlignment:NSTextAlignmentLeft];
    [testLabel setNumberOfLines:0]; //0 - infiniy lines if not enough space more
    [testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; // Autolayout rule: The higher the priority, the more important hold the text.. by horizontal
    [testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//some like above but verticale
    [testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];// reverse like Hugging but Compression. then lower priority then text croping.
    [testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
    [testLabel preferredMaxLayoutWidth:200];//Start new line each 200 points width
    [testLabel setPreferredMaxLayoutWidth:YES];// resize Font size by label size.
    [testLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; // Disable autolaresize, enable autolayouts
Bimawa
  • 3,535
  • 2
  • 25
  • 45
2

There is a couple of steps that you have to do to achieve this using autolayout.

  1. Set layout constrains for the label.
  2. Set height constraint with low priority.
  3. Set numberOfLines to 0 to allow multiline text.
  4. Set preferredMaxLayoutWidth for the label.

The preferredMaxLayoutWidth is used by label to calculate its height.

This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
1

Hi you can manage the width of lable by doing so programatically,

CGRect frame;

frame =self.yourLabel.frame;
frame.size.width +=20;
self.yourLabel.frame=frame;

Thanks

Harunmughal
  • 367
  • 1
  • 4