label根据文字长度改变字体大小 ios

UILabel提供了一个非常方便的属性adjustsFontSizeToFitWidth,当设置为true时,UILabel会自动调整其字体大小以适应其宽度。

 

    UILabel *alabel = [[UILabel alloc] init];
    alabel.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 20);
    alabel.font = [UIFont systemFontOfSize:14];
    alabel.textColor = [UIColor redColor];
    alabel.text = @"UILabel提供了一个非常方便的属性adjustsFontSizeToFitWidth,当设置为true时,UILabel会自动调整其字体大小以适应其宽度。";
    [alabel setAdjustsFontSizeToFitWidth:YES];
    [alabel setMinimumScaleFactor:0.3];
    [self.view addSubview:alabel];

 

posted @ 2025-02-27 14:20  黄增松  阅读(52)  评论(0)    收藏  举报