iOS:调整所有UILabel字体的大小以匹配



我有几个充满文本的UILabels。

UILabel 中的所有字体都设置为自动收缩。

如果其中一个 UILabel 包含太多文本(并且必须自动调整大小),那么如何更改剩余的 UILabels 字体大小以匹配已调整大小的字体大小?

在我的代码末尾,我使用了以下内容:

 if (self.label.font.pointSize < 16){
        labelTwo.font = [UIFont fontWithName:@"Noteworthy-Bold" size:self.label.font.pointSize];

我的默认字体大小是 16,所以我正在检查字体大小是否已减小。

我现在不在Mac上,所以我不能为你编写实际的代码,但我可以为你做假代码,这样你就知道该往哪个方向走。

Create a variable to store the smallest font size.
Foreach for each UILabel in the view.
Get the font size.
Check the font size against the smallest known so far.
    If the font size is smaller than the smallest known, save it.
    Else, disregard.
After all UILabels in view have been checked go back through them, and set the font size to that of the saved value

这就是我实现它的方式

CGFloat fontSize = self.label1.font.pointSize;
CGFloat fontSize1 = self.label2.font.pointSize;
CGFloat fontSize2 = self.label3.font.pointSize;
NSArray *numbers = @[[NSNumber numberWithFloat:fontSize],[NSNumber numberWithFloat:fontSize1],[NSNumber numberWithFloat:fontSize2]];
float xmax = -MAXFLOAT;
float xmin = MAXFLOAT;
for (NSNumber *num in numbers) {
    float x = num.floatValue;
    if (x < xmin) xmin = x;
    if (x > xmax) xmax = x;
    NSLog(@"lowest number = %f",xmin);

最新更新