【问题标题】:Dynamically changing font size of text without changing UILabel size在不改变 UILabel 大小的情况下动态改变文本的字体大小
【发布时间】:2014-10-07 18:46:01
【问题描述】:

我正在开发一个应用程序,其中我有固定大小的UILabel,它可能包含任何大小的文本。 我需要在不改变 UILabel 大小的情况下调整字体大小。

以下是我尝试实现这一目标的方法:

UILabel *errorMessageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,50,300,30)];
    errorMessageLabel.lineBreakMode =NSLineBreakByTruncatingTail;
    errorMessageLabel.numberOfLines = 0;
    errorMessageLabel.adjustsFontSizeToFitWidth=YES;
    errorMessageLabel.adjustsLetterSpacingToFitWidth=YES;
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
        [errorMessageLabel setTextAlignment:NSTextAlignmentCenter];
    }
    else{
        [errorMessageLabel setTextAlignment:UITextAlignmentCenter];
        errorMessageLabel.minimumFontSize=5.0;
    }
    [errorMessageLabel setTag:405];
    [errorMessageLabel setFont:[UIFont systemFontOfSize:13]];
    [backgroundView addSubview:errorMessageLabel];

上述解决方案适用于 iOS 7,但当我尝试在 iOS 6 上运行它时,它会正确更改字体大小,但将文本对齐设置为左对齐,而忽略其中心对齐。

谁能告诉我如何实现这一目标?我试图搜索其他解决方案,但没有任何效果。我无法更改UILabel 的大小

【问题讨论】:

    标签: ios7 ios6 uilabel uifont


    【解决方案1】:
    errorMessageLabel.numberOfLines = 1;
    errorMessageLabel.adjustsFontSizeToFitWidth = YES;
    errorMessageLabel.minimumFontSize = MIN_FONT_SIZE;
    

    【讨论】:

    • 这个我已经包含在我的UILabel 中,但对我不起作用。如果我删除 errorMessageLabel.adjustsLetterSpacingToFitWidth=YES; 它不会调整文本大小
    • numberOfLines 属性设置为 1。
    • 文本在某些情况下可能需要多行显示
    猜你喜欢
    • 2011-06-19
    • 2013-05-14
    • 1970-01-01
    • 2013-08-24
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多