2010-08-19 90 views
5

這是iPhone應用程序的一部分,但應適用於一般用objC編寫的可可。調整UILabel以適應Word Wrap

我有一個UILabel持有不同數量的文本(從單個字符到幾個句子)。文本應始終以最大可能的字體顯示,以適合UILabel中的所有文本。 最大行數設置爲4,換行符模式設置爲自動換行。

由於使用了多行,因此adjustsFontSizeToFitWidth不適用於調整文本大小。

因此我使用一個循環來確定每個字符串最大可能的字體大小,例如:

//Set the text 
    self.textLabel.text = text; 
    //Largest size used 
    NSInteger fsize = 200; textLabel.font = [UIFont 
    fontWithName:@"Verdana-Bold" 
    size:fsize]; 

    //Calculate size of the rendered string with the current parameters 
    float height = [text sizeWithFont:textLabel.font 
     constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) 
     lineBreakMode:UILineBreakModeWordWrap].height; 

    //Reduce font size by 5 while too large, break if no height (empty string) 
    while (height > textLabel.bounds.size.height and height != 0) { 
     fsize -= 5; 
     textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize]; 
     height = [text sizeWithFont:textLabel.font 
      constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) 
      lineBreakMode:UILineBreakModeWordWrap].height; 
    }; 

這種方法適用於大部分。 例外是長詞。 讓我們看看字符串@「體驗富。」舉個例子。 單詞「experience」比其他單詞長得多,不會被正確包裝,而是將字符串分成4行。 我正在尋找一種方法進一步縮小尺寸,以便每個單詞適合一行。

實施例:

-old-

字體大小:60

The 
Exper 
ience 
foo 

應該是

-NEW-

字體大小:30

The 
Experience 
foo 

有可能是一個簡單的方法來做到這一點,但我打牆。

回答

11

這裏是最優雅的(但有點hackish的)的方式,我發現,使這項工作:

  1. 分割字符串成字
  2. 使用當前字體大小
  3. 計算每個字的寬度減少字符串的大小,直到每個詞適合一行

資源消耗足夠低,這個工作即使在UITableViews滿弦的EDI這樣。

這是新代碼:

//Set the text 
self.textLabel.text = text; 
//Largest size used 
NSInteger fsize = 200; textLabel.font = [UIFont fontWithName:@"Verdana-Bold" 
                 size:fsize]; 

//Calculate size of the rendered string with the current parameters 
float height = 
     [text sizeWithFont:textLabel.font 
     constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) 
      lineBreakMode:UILineBreakModeWordWrap].height; 

//Reduce font size by 5 while too large, break if no height (empty string) 
while (height > textLabel.bounds.size.height and height != 0) { 
    fsize -= 5; 
    textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize]; 
    height = [text sizeWithFont:textLabel.font 
       constrainedToSize:CGSizeMake(textLabel.bounds.size.width,99999) 
        lineBreakMode:UILineBreakModeWordWrap].height; 
}; 

// Loop through words in string and resize to fit 
for (NSString *word in [text componentsSeparatedByString:@" "]) { 
    float width = [word sizeWithFont:textLabel.font].width; 
    while (width > textLabel.bounds.size.width and width != 0) { 
     fsize -= 3; 
     textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:fsize]; 
     width = [word sizeWithFont:textLabel.font].width; 

    } 
} 
+0

因爲'sizeWithFont:'已經成爲過時與iOS 7,你應該用'boundingRectWithSize:'替換它。 – AppsolutEinfach 2014-10-16 17:05:33

3

您可以在一個類別使用的代碼上面的UILabel

的UILabel + AdjustFontSize。^ h

@interface UILabel (UILabel_AdjustFontSize) 

- (void) adjustsFontSizeToFitWidthWithMultipleLinesFromFontWithName:(NSString*)fontName size:(NSInteger)fsize andDescreasingFontBy:(NSInteger)dSize; 

@end 

的UILabel + AdjustFontSize.m

@implementation UILabel (UILabel_AdjustFontSize) 

- (void) adjustsFontSizeToFitWidthWithMultipleLinesFromFontWithName:(NSString*)fontName size:(NSInteger)fsize andDescreasingFontBy:(NSInteger)dSize{ 

    //Largest size used 
    self.font = [UIFont fontWithName:fontName size:fsize]; 

    //Calculate size of the rendered string with the current parameters 
    float height = [self.text sizeWithFont:self.font 
        constrainedToSize:CGSizeMake(self.bounds.size.width,99999) 
         lineBreakMode:UILineBreakModeWordWrap].height; 

    //Reduce font size by dSize while too large, break if no height (empty string) 
    while (height > self.bounds.size.height && height != 0) { 
     fsize -= dSize; 
     self.font = [UIFont fontWithName:fontName size:fsize]; 
     height = [self.text sizeWithFont:self.font 
        constrainedToSize:CGSizeMake(self.bounds.size.width,99999) 
         lineBreakMode:UILineBreakModeWordWrap].height; 
    }; 

    // Loop through words in string and resize to fit 
    for (NSString *word in [self.text componentsSeparatedByString:@" "]) { 
     float width = [word sizeWithFont:self.font].width; 
     while (width > self.bounds.size.width && width != 0) { 
      fsize -= dSize; 
      self.font = [UIFont fontWithName:fontName size:fsize]; 
      width = [word sizeWithFont:self.font].width;    
     } 
    } 
} 

@end 
+1

試過你的代碼,在while語句中進入無限循環。 – Resh32 2012-10-01 08:40:53

3

這是我在一個類別的0x90的回答版本:

@implementation UILabel (MultilineAutosize) 

- (void)adjustFontSizeToFit 
{ 
    //Largest size used 
    NSInteger fsize = self.font.pointSize; 

    //Calculate size of the rendered string with the current parameters 
    float height = [self.text sizeWithFont:self.font 
         constrainedToSize:CGSizeMake(self.bounds.size.width, MAXFLOAT) 
          lineBreakMode:NSLineBreakByWordWrapping].height; 

    //Reduce font size by 5 while too large, break if no height (empty string) 
    while (height > self.bounds.size.height && height > 0) { 
     fsize -= 5; 
     self.font = [self.font fontWithSize:fsize]; 
     height = [self.text sizeWithFont:self.font 
         constrainedToSize:CGSizeMake(self.bounds.size.width, MAXFLOAT) 
          lineBreakMode:NSLineBreakByWordWrapping].height; 
    }; 

    // Loop through words in string and resize to fit 
    for (NSString *word in [self.text componentsSeparatedByString:@" "]) { 
     float width = [word sizeWithFont:self.font].width; 
     while (width > self.bounds.size.width && width > 0) { 
      fsize -= 3; 
      self.font = [self.font fontWithSize:fsize]; 
      width = [word sizeWithFont:self.font].width; 
     } 
    } 
} 

@end