Подтвердить что ты не робот

Как я могу сделать текст в размере шрифта утилит UILabel

Если UILabel содержит слишком много текста, как настроить ярлык, чтобы он уменьшал размер шрифта?

Вот как я настраиваю свой UILabel:

     descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
    [descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
    [descriptionLabel setBackgroundColor:[UIColor clearColor]];
    [descriptionLabel setTextColor:[UIColor whiteColor]];
    descriptionLabel.numberOfLines = 1;
    [self addSubview:descriptionLabel];
4b9b3361

Ответ 1

descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously

Следующий пример проверяется и проверяется на iPhone Simulator 3.1.2:

UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 200, 30)];

descriptionLabel.font = [UIFont systemFontOfSize:14.0];
descriptionLabel.minimumFontSize = 10.0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.numberOfLines = 1;
descriptionLabel.text = @"supercalifragilisticexpialidocious even thought he sound of it is something quite attrocious";

Ответ 2

Чтобы изменить размер текста в многострочном UILabel, вы можете использовать этот вспомогательный метод (на основе code из 11 Pixel Studios):

+ (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize { 
 // use font from provided label so we don't lose color, style, etc
 UIFont *font = aLabel.font;

 // start with maxSize and keep reducing until it doesn't clip
 for(int i = maxSize; i >= minSize; i--) {
  font = [font fontWithSize:i];
  CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT);

  // This step checks how tall the label would be with the desired font.
  CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
  if(labelSize.height <= aLabel.frame.size.height)
   break;
 }
 // Set the UILabel font to the newly adjusted font.
 aLabel.font = font;
}

Ответ 4

Если вы хотите, чтобы количество строк также увеличивалось, если необходимо, используйте решение Steve N с инструкцией if:

if(labelSize.height <= aLabel.frame.size.height)
{
  aLabel.numberOfLines = labelSize.height / font.lineHeight;

  break;
}