iOS -- 设置label的自适应

- (void)AutoLabel {
    
    //准备工作
    
    self.font = [UIFont systemFontOfSize:17];
    
    self.textColor = [UIColor whiteColor];
    
    self.layer.cornerRadius = 4;
    
    self.layer.masksToBounds = YES;
    
    self.numberOfLines = 0;//根据最大行数需求来设置
    
    self.lineBreakMode = NSLineBreakByTruncatingTail;
    
    CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
    
    //关键语句
    
    CGSize expectSize = [self sizeThatFits:maximumLabelSize];
    
    //别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值
    
    self.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);
    
}
原文地址:https://www.cnblogs.com/mafeng/p/7595115.html