2012-03-07 172 views

回答

6

是,的UILabel可以为你做的,只是做:

theLabel.adjustsFontSizeToFitWidth = YES; 
theLabel.minimumFontSize = MIN_FONT_SIZE; 

注意这个(从文件):

只有当numberOfLines属性设置 此属性是有效的为1.

+0

我加 [qLabel setNumberOfLines:1]; [qLabel setAdjustsFontSizeToFitWidth:YES]; [qLabel setMinimumFontSize:15]; to viewDidLoad,但文本显示在最后的单行上。我想念什么? – OhDoh 2012-03-07 19:19:59

+0

然后文本的最小字体大小...尝试降低最小值。 – fbernardo 2012-03-07 19:23:21

+0

我设置了[qLabel setNumberOfLines:0],它的确有窍门。谢谢 ! – OhDoh 2012-03-07 19:27:32

1

我在一个点创建了一个类别方法。你基本上喂它一个矩形,它会返回一个适合的字体。也许你可以从下面的例子原油搜集的东西:

- (UIFont *)fontSizeForRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode minFontSize:(CGFloat)minFontSize 
    { 

      CGFloat fontSize = [font pointSize]; 
      UIFont *tempFont = [UIFont fontWithName:[font fontName] size:[font pointSize]]; 
      CGFloat acceptableFontSize = fontSize; 
      while (fontSize > minFontSize) 
      { 
       UIFont *testFont = [UIFont fontWithName:[tempFont fontName] size:fontSize]; 
       CGSize sizeWithTestFont = [self sizeWithFont:testFont constrainedToSize:CGSizeMake(rect.size.width, 99999.0) lineBreakMode:lineBreakMode]; 
       if (sizeWithTestFont.height > rect.size.height) 
        fontSize -= 1.0f; //Shrink the font size by a point 
       else 
       { 
        //Fits. Use it. 
        acceptableFontSize = fontSize; 
        break; 
       } 
      } 

      return [UIFont fontWithName:[font fontName] size:acceptableFontSize]; 
     } 
相关问题