這是一個非常基本的問題,但我無法找到適當的解決方案。我有幾個圈子裏面有文字,就像你在圖片中看到的一樣。文本被動態加載並且具有從一個單詞到五個單詞或更多的大小。目標是將文字儘可能放大到圈子中。新行可以出現,但每個單詞應該保持在一起。示例圖像是確定的,但我希望文本更大,因爲文本和圓圈之間仍有一些空閒空間。圓是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];
編輯: 所以,我想Rufel的解決方案。我認爲這種縮小的作品,但我的話被撕碎了。即使我有buttonlabel.lineBreakMode = NSLineBreakByWordWrapping;
它看起來像這樣:
這是我的代碼。我還實現了答案中提到的其他方法。
//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];
我的答案已更新,以防止字詞截斷,因爲''''boundingRectForString:'''總是返回一個等於或小於所提供的寬度的寬度,即使單個字寬度大於該寬度。 – Rufel