适合 UILabel 中的文本
这是我的代码
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...
问题是字体很大并且无法适合标签。它只是显示“A very”
What to do,以便显示整个文本。 我已经尝试过
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
但它对我不起作用。 我想以编程方式做到这一点。
//编辑
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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为您只需要添加以下内容:
然后文本将自动调整大小以适合标签。
但请注意,只有当 label.numberOfLines = 1 时,这才会真正起作用,这样文本就在一行上。
如果您需要文本换行但仍缩小以适合,则解决方案会更复杂。为此,您需要计算文本的渲染大小,然后在循环中减小它,如下所示:
这基本上只是减小字体大小,直到适合标签。
更新:
从 iOS7 开始,当
adjustsFontSizeToFitWidth = YES
时,多行文本也会自动缩小,因此不再需要这个答案的第二部分(除非您仍然支持 iOS 6 和较早)。I think you just need to add this:
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.
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:
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).最后我得到了阿拉伯语文本对齐问题的解决方案,你只需这样做:
Finally I got solution for text allignment issue in arabic language you just do like this:
它会解决你的问题。
It will work for your problem.
使用 iOS 9 的 Swift
这不会增加字体大小来填充标签。它只是从最大尺寸开始,并根据需要减小到最小尺寸。这也是假设行数为 1。
Swift with iOS 9
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.
Interface Builder 现在就可以让您执行此操作。
或者,您可以通过编程方式执行此操作:
Interface Builder lets you do this now.
Alternatively you can do it programmatically:
iOS 8 中的一切似乎都被破坏了(可能 iOS 7 也是如此)。
解决方案:
确保您不要尝试使用
label.adjustsFontSizeToFitWidth = YES
,否则它会变得非常混乱,并且新尺寸将无法正常工作。Everything seems to be broken in iOS 8 (probably iOS 7 too).
Solution:
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.