iOS UILabel 自动适应文字的长度

    UILabel *dateLabel = [UILabel new];
    dateLabel.textColor = [UIColor whiteColor];
    dateLabel.font = [UIFont systemFontOfSize:16.f];
    dateLabel.text = strDate;
//设置这句代码可以使控件自适应文字的长度
    [dateLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.view addSubview:dateLabel];
    
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(30);
        make.left.equalTo(self).with.offset(10);
        make.height.equalTo(@19);
    }];

你可能感兴趣的:(iOS UILabel 自动适应文字的长度)