UILabel自适应文字大小

来源:互联网 发布:算法导论 pdf 编辑:程序博客网 时间:2024/05/21 17:26

 UILabel *_lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

        [_lab setText:msgStr];

        //设置自动行数与字符换行

        [_lab setNumberOfLines:0];

        _lab.lineBreakMode = NSLineBreakByWordWrapping;

//        背景颜色

        _lab.backgroundColor = [UIColor colorWithRed:32.0/255.0 green:90.0/255.0 blue:167.0/255.0 alpha:0.9];

        _lab.textColor = [UIColor whiteColor];

        //设置一个行高上限

        CGSize size = CGSizeMake(320,2000);

        _lab.font = [UIFont fontWithName:@"Helvetica" size:16.0];

        _lab.adjustsFontSizeToFitWidth = YES;

        CGSize labelsize = [msgStr sizeWithFont:_lab.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];

        _lab.frame = CGRectMake((320-labelsize.width)/2, 320, labelsize.width, labelsize.height);

        self.lab = _lab;

        

        [self performSelector:@selector(dismisslab) withObject:nil afterDelay:2.0f];

0 0