Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UILabel width based on text length programatically

I would like to create a UILabel that is a little bit wider than the width of the text in the same UILabel. I checked a lot of questions and tried the answers from them, but still can't find the right way. I can get the width of the text, but can't redrawn the frame.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

static NSString *Cell = @"cell";
LikeActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];

cell.lbl.backgroundColor = [UIColor colorWithRed:7.0 green:4.0 blue:55.0 alpha:0.7];

NSString *lblString = [NSString stringWithFormat:@"%@", object[@"content"]];
cell.lbl.text = lblString;

float widthIs =  [cell.lbl.text
         boundingRectWithSize:cell.lbl.frame.size
         options:NSStringDrawingUsesLineFragmentOrigin
         attributes:@{ NSFontAttributeName:cell.lbl.font }
         context:nil]
        .size.width;


    float heightIs =
        [cell.lbl.text
         boundingRectWithSize:cell.lbl.frame.size
         options:NSStringDrawingUsesLineFragmentOrigin
         attributes:@{ NSFontAttributeName:cell.lbl.font }
         context:nil]
        .size.height;
NSLog(@"CELL TEXT WIDTH %f", widthIs);

int x = widthIs + 10;
int y = heightIs;
// ATTEMPT 1   
[cell.lbl setFrame:CGRectMake(0,0,x,cell.lbl.frame.origin.y)];

// ATTEMPT 2
cell.lbl.frame = CGRectMake(0, 0, x, cell.lbl.frame.origin.y);

return cell;

}

When I run these lines nothing happens, the label doesn't change that I can't understand because it should.

like image 680
rihe Avatar asked Nov 30 '22 10:11

rihe


1 Answers

You can have the width of the label wrap around the text with just auto layout. If the text is only one line and you want the label size to fit around that, all you have to do is add a constraint that sets the leading attribute or center X (and constraints for the top/Center Y and height) and make sure you add

[yourLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[yourLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

after adding it to the subview. If the text is multiline or could be multiline you will need a few more lines of code. You will need to set the compression resistance and hug for the vertical axes,

[yourLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [yourLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

Also add

- (void)layoutSubviews {
    if (!CGRectIsEmpty(self.frame)) {
        _featureDescriptionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.contentView.frame)-59-5;
    }
    [super layoutSubviews];
}

This goes inside your custom table view cell. Set the max layout width to the widest width the text can be inside of your cell.

Also if it is multiline don't forget to set the number of lines to 0.

like image 96
Maximilian Litteral Avatar answered Dec 05 '22 13:12

Maximilian Litteral