Ios 将文本放在尽可能大的圆圈中
这是一个相当基本的问题,但我找不到合适的解决办法。我有几个圆圈,里面有文字,如图中所示。文本动态加载,大小从一个单词到五个单词或更多。目标是将文本尽可能大地放入圆圈中。可以出现新行,但每个单词都应该保持在一起。示例图像有点不错,但我更希望文本更大,因为文本和圆圈之间仍然有一些自由空间。圆圈是80x80。我尝试过的所有解决方案都是将文本剪得太窄或文本太小 如何创建标签:Ios 将文本放在尽可能大的圆圈中,ios,objective-c,Ios,Objective C,这是一个相当基本的问题,但我找不到合适的解决办法。我有几个圆圈,里面有文字,如图中所示。文本动态加载,大小从一个单词到五个单词或更多。目标是将文本尽可能大地放入圆圈中。可以出现新行,但每个单词都应该保持在一起。示例图像有点不错,但我更希望文本更大,因为文本和圆圈之间仍然有一些自由空间。圆圈是80x80。我尝试过的所有解决方案都是将文本剪得太窄或文本太小 如何创建标签: UILabel *buttonlabel = [[UILabel alloc] initWithFrame:CGRectMake
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;
}
我在这里找到了这条线索:
这也有同样的问题
编辑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];
我认为,您要做的是询问NSString的
boundingRectWithSize:options:attributes:context:
。通过设置边界矩形的宽度,可以确定其高度。可以使用圆的参数化公式来确定边界矩形是否完全适合圆的中心。不幸的是,您将不得不执行一种尝试和错误的近似顺序,文本变得越来越大,直到顶部和底部伸出圆圈,然后缩小建议的宽度,看看这是否会导致高度增长过多,因为文本现在包装了一段额外的时间。我想您想做什么,是询问NSString的boundingRectWithSize:options:attributes:context:
。通过设置边界矩形的宽度,可以确定其高度。可以使用圆的参数化公式来确定边界矩形是否完全适合圆的中心。不幸的是,您将不得不执行一种尝试和错误的近似顺序,文本变得越来越大,直到顶部和底部伸出圆圈,然后缩小建议的宽度,看看这是否会导致高度增长过多,因为文本现在包装了一段额外的时间。我想您想做什么,是询问NSString的boundingRectWithSize:options:attributes:context:
。通过设置边界矩形的宽度,可以确定其高度。可以使用圆的参数化公式来确定边界矩形是否完全适合圆的中心。不幸的是,您将不得不执行一种尝试和错误的近似顺序,文本变得越来越大,直到顶部和底部伸出圆圈,然后缩小建议的宽度,看看这是否会导致高度增长过多,因为文本现在包装了一段额外的时间。我想您想做什么,是询问NSString的boundingRectWithSize:options:attributes:context:
。通过设置边界矩形的宽度,可以确定其高度。可以使用圆的参数化公式来确定边界矩形是否完全适合圆的中心。不幸的是,你将不得不执行一种尝试和错误的近似序列,文本变得越来越大,直到顶部和底部伸出圆圈,然后缩小建议的宽度,看看这是否会导致高度增长过多,因为文本现在需要额外包装一段时间。假设您有一个自定义视图,在该视图中绘制一个适合其框架的圆(示例中为80x80)
您首先需要找到标签在字母不穿过圆圈的情况下所能承受的最大宽度:
- (CGFloat)buttonLabelMaxWidth
{
CGFloat hypotenuse = CGRectGetWidth(self.bounds);
CGFloat rightTriangleCathetus = sqrtf((hypotenuse*hypotenuse)/2);
return floorf(rightTriangleCathetus);
}
接下来,当您传递要显示的标题时,您将希望通过减少最初过大的字体进行迭代,直到生成的字符串边界符合之前计算的宽度(这也是最大高度,因为它是一个圆)更新:您还需要检查标题中的每个单词,以确保它们没有被截断(它们符合最大宽度)
-(void)setButtontTitle:(NSString*)标题
{
CGFloat fontSize=20;//要使用的最大字体大小
CGFloat minimumFontSize=5;//要使用的最小字体大小
CGFloat labelHeightWithFont=0;
CGFloat longestWordWidth=0;
UIFont*labelFont=nil;
CGFloat按钮LabelMaxWidth=[自按钮LabelMaxWidth];
做{
如果(字体大小<最小字体大小){
//处理标题不合适的异常
打破
}
//尝试当前字体大小(如果合适)
labelFont=[UIFont systemFontOfSize:fontSize--];
CGSize boundingSize=[self-boundingSizeForString:title-font:labelFont];
labelHeightWithFont=boundingSize.height;
//确保单词没有被截断(它们符合最大宽度)
longestWordWidth=0;
对于(在[title components separatedbycharactersinset:[NSCharacterSet whitespaceAndNewlineCharacterSet]]中的NSString*单词){
CGSize wordSize=[word size属性:@{NSFontAttributeName:labelFont}];
longestWordWidth=MAX(longestWordWidth,wordSize.width);
}
//循环,直到当前大小的文本符合最大宽度/高度。
}while(标签高度,字体>
- (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));
}