ios 适合 UILabel 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9101607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fit text in UILabel
提问by objlv
Here is my code
这是我的代码
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 300, 50)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textColor.font = [UIFont fontWithName:@"Verdana" size:30];
label.text = @"A very long string";
etc...
The problems is that the font is large and can't fit in the label. It just display "A very"
问题是字体很大,无法放入标签。它只显示“非常”
What to do so entire text to be displayed. I have tried
怎么做,让整个文本都显示出来。我试过了
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
But it doesn't work for me. I want to do that programmatically.
但这对我不起作用。我想以编程方式做到这一点。
//EDIT
//编辑
CGRect frame = CGRectMake(10, 50, 300, 50);
NSString *labelString = @"Players.";
UILabel *howManyUsersLabel = [[UILabel alloc]initWithFrame:frame];
howManyUsersLabel.textAlignment = UITextAlignmentCenter;
howManyUsersLabel.backgroundColor = [UIColor clearColor];
howManyUsersLabel.textColor = [UIColor whiteColor];
howManyUsersLabel.adjustsFontSizeToFitWidth = NO;
howManyUsersLabel.numberOfLines = 0;
CGFloat fontSize = 30;
while (fontSize > 0.0)
{
CGSize size = [labelString sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= frame.size.height) break;
fontSize -= 1.0;
NSLog(@"test");
}
howManyUsersLabel.font = [UIFont fontWithName:@"Verdana" size:fontSize];
回答by Nick Lockwood
I think you just need to add this:
我认为你只需要添加这个:
label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 0;
Then the text will automatically resize to fit the label.
然后文本将自动调整大小以适合标签。
Note however that this will only really work if the label.numberOfLines = 1, so that the text is on a single line.
但是请注意,这仅在 label.numberOfLines = 1 时才真正起作用,因此文本位于一行上。
If you need the text to wrap onto multiple lines but still shrink to fit, the solution is more complex. To do this, you need to calculate the rendered size of the text and then reduce it in a loop, as follows:
如果您需要将文本换行到多行但仍然缩小以适应,则解决方案更复杂。为此,您需要计算文本的渲染大小,然后循环缩小,如下所示:
NSString *theText = @"A long string";
CGRect labelRect = CGRectMake(10, 50, 300, 50);
label.adjustsFontSizeToFitWidth = NO;
label.numberOfLines = 0;
CGFloat fontSize = 30;
while (fontSize > 0.0)
{
CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= labelRect.size.height) break;
fontSize -= 1.0;
}
//set font size
label.font = [UIFont fontWithName:@"Verdana" size:fontSize];
This basically just reduces the font size until it fits the label.
这基本上只是减小字体大小,直到它适合标签。
UPDATE:
更新:
As of iOS7, multiline text will also shrink automatically when adjustsFontSizeToFitWidth = YES
, so the second part of this answer is no longer needed (unless you still support iOS 6 and earlier).
从 iOS7 开始,多行文本也会在 时自动缩小adjustsFontSizeToFitWidth = YES
,因此不再需要此答案的第二部分(除非您仍然支持 iOS 6 及更早版本)。
回答by Maishi Wadhwani
Finally I got solution for text allignment issue in arabic language you just do like this:
最后,我得到了阿拉伯语文本对齐问题的解决方案,您只需这样做:
label.text = @"??? ?? ?? ???? ???";
label.textAlignment = NSTextAlignmentNatural;
CGSize size = [labels sizeThatFits:CGSizeMake(_lblAddress.width, CGFLOAT_MAX)];
label.height = size.height;
回答by Suragch
Swift with iOS 9
Swift 与 iOS 9
let maxFontSize: CGFloat = 40
let minFontSize: CGFloat = 10
label.font = UIFont(name: label.font.fontName, size: maxFontSize)!
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = minFontSize/maxFontSize
This doesn't increase the font size to fill the label. It just starts with the max size and decreases as necessary down to the minimum. This is also assuming that the number of lines is 1.
这不会增加字体大小来填充标签。它只是从最大尺寸开始,然后根据需要减小到最小尺寸。这也是假设行数为 1。
回答by Maishi Wadhwani
[UILabel sizeToFit];
It will work for your problem.
它会解决你的问题。
回答by Piyush Kashyap
CGRect titleRect = CGRectMake(10, 50, 300, 50);
UILabel *textTitleView = [[UILabel alloc] initWithFrame:titleRect];
textTitleView.numberOfLines = 3 //for multiple lines;
textTitleView.lineBreakMode = UILineBreakModeWordWrap;
[UIFont fontWithName:@"Verdana" size:30];
textTitleView.text = @"your text";
回答by Jeffrey Sun
Interface Builder lets you do this now.
Interface Builder 可让您立即执行此操作。
- In UILabel, under Autoshrink, select "Minimum Font Size"instead of "Fixed Font Size".
- Set the Minimum Font Sizeto be something reasonable, like 8.
- You can also check the checkmark "Tighten Letter Spacing".
- 在 UILabel 中,在 Autoshrink 下,选择“最小字体大小”而不是“固定字体大小”。
- 将最小字体大小设置为合理的值,例如 8。
- 您还可以勾选“收紧字母间距”。
Alternatively you can do it programmatically:
或者,您可以以编程方式执行此操作:
label.adjustsFontSizeToFitWidth = YES;
回答by brandonscript
Everything seems to be broken in iOS 8 (probably iOS 7 too).
iOS 8(也可能是iOS 7)中的一切似乎都被破坏了。
Solution:
解决方案:
-(UIFont*)fontForString:(NSString*)string toFitInRect:(CGRect)rect seedFont:(UIFont*)seedFont {
UIFont* returnFont = seedFont;
CGSize stringSize = [string sizeWithAttributes:@{NSFontAttributeName : seedFont}];
while(stringSize.width > rect.size.width){
returnFont = [UIFont systemFontOfSize:returnFont.pointSize -1];
stringSize = [string sizeWithAttributes:@{NSFontAttributeName : returnFont}];
}
return returnFont;
}
Make sure you don't try and use label.adjustsFontSizeToFitWidth = YES
otherwise it'll get really confused and the new size won't work properly.
确保您不要尝试和使用,label.adjustsFontSizeToFitWidth = YES
否则它会变得非常混乱并且新尺寸将无法正常工作。