2015-04-25 17 views
0

這是一個非常基本的問題,但我無法找到適當的解決方案。我有幾個圈子裏面有文字,就像你在圖片中看到的一樣。文本被動態加載並且具有從一個單詞到五個單詞或更多的大小。目標是將文字儘可能放大到圈子中。新行可以出現,但每個單詞應該保持在一起。示例圖像是確定的,但我希望文本更大,因爲文本和圓圈之間仍有一些空閒空間。圓是80x80。我試過的所有解決方案都是蠻橫地裁剪文本或文本太小。將文字置於儘可能大的圓上

如何創建標籤:

UILabel *buttonlabel = [[UILabel alloc] initWithFrame:CGRectMake(12,7,57,64)]; 

    [buttonlabel setText: @"Recipes"]; 
    buttonlabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18.0f]; 
    buttonlabel.textColor = [UIColor whiteColor]; 
    buttonlabel.textAlignment = NSTextAlignmentCenter; 
    buttonlabel.lineBreakMode = NSLineBreakByWordWrapping; 
    buttonlabel.numberOfLines = 3; 
    [button addSubview:buttonlabel]; 
    [buttonlabel release]; 

enter image description here

編輯: 所以,我想Rufel的解決方案。我認爲這種縮小的作品,但我的話被撕碎了。即使我有buttonlabel.lineBreakMode = NSLineBreakByWordWrapping;

它看起來像這樣:

enter image description here

這是我的代碼。我還實現了答案中提到的其他方法。

//Create the button labels 
    UILabel *buttonlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; 


    [buttonlabel setText: @"text"; 
    buttonlabel.textColor = [UIColor whiteColor]; 
    buttonlabel.textAlignment = NSTextAlignmentCenter; 
    buttonlabel.lineBreakMode = NSLineBreakByWordWrapping; 

    buttonlabel.numberOfLines = 0; 

    CGFloat fontSize = 20; // The max font size you want to use 
    CGFloat labelHeightWithFont = 0; 
    UIFont *labelFont = nil; 

    do { 
     // Trying the current font size if it fits 
     labelFont = [UIFont systemFontOfSize:fontSize--]; 
     CGRect boundingRect = [self boundingRectForString:subcatbuttontitlesarray[buttonTag-1] font:labelFont]; 
     labelHeightWithFont = boundingRect.size.height; 
     // Loop until the text at the current size fits the maximum width/height. 
    } while (labelHeightWithFont > [self buttonLabelMaxWidth]); 

    buttonlabel.text = subcatbuttontitlesarray[buttonTag-1]; 
    buttonlabel.font = labelFont; 

- (CGRect)boundingRectForString:(NSString *)string font:(UIFont *)font 
{ 

return [string boundingRectWithSize:CGSizeMake([self buttonLabelMaxWidth], MAXFLOAT) 
             options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading 
            attributes:@{NSFontAttributeName: font} 
             context:nil]; 
} 



- (CGFloat)buttonLabelMaxWidth 
     { 
    CGFloat hypotenuse = CGRectGetWidth(CGRectMake(0, 0, 60, 60)); 
    CGFloat rightTriangleCathetus = sqrtf((hypotenuse*hypotenuse)/2); 
    return rightTriangleCathetus; 
     } 

我在這裏找到這個線程:

iOS7 - Adjusting font size of multiline label to fit its frame

它有同樣的問題。

編輯2:

尋找一個完整的一天解決方案,並嘗試各種標籤組合的屬性後不知何故,我想通了,在「numberoflines」是我的罪魁禍首。於是我想出了在字符串中計算的話,調整線路的基礎上,弦數數這個愚蠢的解決方案:

NSString *samplestring = @"Three words string"; 
//Count the words in this string 
int times = [[samplestring componentsSeparatedByString:@" "] count]-1; 

UILabel *testlabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, 60, 60)]; 
[testlabel setText:samplestring]; 



[testlabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:40.0f]]; 
[testlabel setBackgroundColor:[UIColor redColor]]; 

[testlabel setAdjustsFontSizeToFitWidth:YES]; 
    [testlabel setTextAlignment:NSTextAlignmentCenter]; 

//My workaround 
if(times ==0){ 
[testlabel setNumberOfLines:1]; 
}else{ 
    if(times==1){ 
     [testlabel setNumberOfLines:2]; 
    } 
     else{ 
      [testlabel setNumberOfLines:3]; 
     }} 




[self.view addSubview:testlabel]; 
+0

我的答案已更新,以防止字詞截斷,因爲''''boundingRectForString:'''總是返回一個等於或小於所提供的寬度的寬度,即使單個字寬度大於該寬度。 – Rufel

回答

1

假設您有一個自定義視圖,您可以在其中繪製一個適合其框架的圓圈(在您的示例中爲80x80)。

首先您要查找的最大寬度的標籤可以不封渡圈:

- (CGFloat)buttonLabelMaxWidth 
{ 
    CGFloat hypotenuse = CGRectGetWidth(self.bounds); 
    CGFloat rightTriangleCathetus = sqrtf((hypotenuse*hypotenuse)/2); 
    return floorf(rightTriangleCathetus); 
} 

接下來,當你通過顯示的標題,你會希望通過降低最初超大迭代字體,直到生成的字符串邊界符合之前計算的寬度(這也是自圓周以來的最大高度)。更新:您還需要檢查標題中的每個單詞以確保它們未被截斷(它們適合最大寬度)。

- (void)setButtonTitle:(NSString *)title 
{ 
    CGFloat fontSize = 20; // The max font size you want to use 
    CGFloat minimumFontSize = 5; // The min font size you want to use 
    CGFloat labelHeightWithFont = 0; 
    CGFloat longestWordWidth = 0; 
    UIFont *labelFont = nil; 

    CGFloat buttonLabelMaxWidth = [self buttonLabelMaxWidth]; 

    do { 

     if (fontSize < minimumFontSize) { 
      // Handle exception where the title just won't fit 
      break; 
     } 

     // Trying the current font size if it fits 
     labelFont = [UIFont systemFontOfSize:fontSize--]; 
     CGSize boundingSize = [self boundingSizeForString:title font:labelFont]; 
     labelHeightWithFont = boundingSize.height; 

     // Be sure that words are not truncated (that they fits in the maximum width) 
     longestWordWidth = 0; 
     for (NSString *word in [title componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) { 
      CGSize wordSize = [word sizeWithAttributes:@{NSFontAttributeName: labelFont}]; 
      longestWordWidth = MAX(longestWordWidth, wordSize.width); 
     } 

     // Loop until the text at the current size fits the maximum width/height. 
    } while (labelHeightWithFont > buttonLabelMaxWidth || longestWordWidth > buttonLabelMaxWidth); 

    self.buttonLabel.text = title; 
    self.buttonLabel.font = labelFont; 
} 

- (CGSize)boundingSizeForString:(NSString *)string font:(UIFont *)font 
{ 

    CGRect boundingRect = [string boundingRectWithSize:CGSizeMake([self buttonLabelMaxWidth], MAXFLOAT) 
           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading 
          attributes:@{NSFontAttributeName: font} 
           context:nil]; 
    return CGSizeMake(ceilf(boundingRect.size.width), ceilf(boundingRect.size.height)); 
} 
+0

我假設你的意思是 while(labelHeightWithFont> [self buttonLabelMaxWidth]); ? – tyler

+0

是的,對不起,我修好了。我從我自己的一個項目的一部分做出了這個答案,並更改了方法/變量名稱以反映您的情況。 – Rufel

+0

我編輯了我的問題。它不能正常工作:(我不小心編輯了你的答案,對不起。 – tyler

2

你想要做什麼,我想,是要求的NSString其boundingRectWithSize:options:attributes:context:。通過設置邊界矩形的寬度,可以找出高度是多少。您可以使用圓的參數公式來確定該邊界矩形是否完全適合圓的中心。不幸的是,你將不得不進行一系列的反覆試驗,文字越來越大,直到頂部和底部突出圓圈,然後縮小建議的寬度並查看這是否導致高度變爲增長太多,因爲文本現在包裝了額外的時間。

0

「setAdjustsFontSizeToFitWidth」類似的方法嗎?好吧,無論如何它不會工作。 Fontsize不會縮小,單詞中間的單詞會被撕開,就像上面的圖片中所看到的一樣。

+1

當''''numberOfLines''設置爲1時,屬性'''setAdjustsFontSizeToFitWidth'''纔有效。去檢查我更新的答案,我添加了一個檢查單詞是否被截斷的循環。它應該工作;) – Rufel

+0

謝謝魯菲爾,我用我的解決方案編輯我的第一個問題,這是愚蠢的我猜,但它適用於我。 – tyler

相關問題