适合 UILabel 中的文本

发布于 2025-01-01 07:03:01 字数 1500 浏览 2 评论 0原文

这是我的代码

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

执妄 2025-01-08 07:03:01

我认为您只需要添加以下内容:

label.adjustsFontSizeToFitWidth = YES;
label.minimumFontSize = 0;

然后文本将自动调整大小以适合标签。

但请注意,只有当 label.numberOfLines = 1 时,这才会真正起作用,这样文本就在一行上。

如果您需要文本换行但仍缩小以适合,则解决方案会更复杂。为此,您需要计算文本的渲染大小,然后在循环中减小它,如下所示:

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];

这基本上只是减小字体大小,直到适合标签。

更新:

从 iOS7 开始,当 adjustsFontSizeToFitWidth = YES 时,多行文本也会自动缩小,因此不再需要这个答案的第二部分(除非您仍然支持 iOS 6 和较早)。

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.

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).

赠意 2025-01-08 07:03:01

最后我得到了阿拉伯语文本对齐问题的解决方案,你只需这样做:

    label.text = @"هذا هو نص طويل جدا";
    label.textAlignment = NSTextAlignmentNatural;

    CGSize size = [labels sizeThatFits:CGSizeMake(_lblAddress.width, CGFLOAT_MAX)];
    label.height = size.height;

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;
月棠 2025-01-08 07:03:01
[UILabel sizeToFit];

它会解决你的问题。

[UILabel sizeToFit];

It will work for your problem.

无远思近则忧 2025-01-08 07:03:01

使用 iOS 9 的 Swift

let maxFontSize: CGFloat = 40
let minFontSize: CGFloat = 10

label.font = UIFont(name: label.font.fontName, size: maxFontSize)!
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = minFontSize/maxFontSize

这不会增加字体大小来填充标签。它只是从最大尺寸开始,并根据需要减小到最小尺寸。这也是假设行数为 1。

Swift with 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.

眼睛会笑 2025-01-08 07:03:01
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";
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";
假面具 2025-01-08 07:03:01

Interface Builder 现在就可以让您执行此操作。

  1. 在 UILabel 中的“自动收缩”下,选择“最小字体大小”,而不是“固定字体大小”。
  2. 最小字体大小设置为合理的值,例如8。
  3. 您还可以选中“收紧字母间距”复选标记。

或者,您可以通过编程方式执行此操作:

label.adjustsFontSizeToFitWidth = YES;

Interface Builder lets you do this now.

  1. In UILabel, under Autoshrink, select "Minimum Font Size" instead of "Fixed Font Size".
  2. Set the Minimum Font Size to be something reasonable, like 8.
  3. You can also check the checkmark "Tighten Letter Spacing".

Alternatively you can do it programmatically:

label.adjustsFontSizeToFitWidth = YES;
寻找我们的幸福 2025-01-08 07:03:01

iOS 8 中的一切似乎都被破坏了(可能 iOS 7 也是如此)。

解决方案:

-(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;
}

确保您不要尝试使用label.adjustsFontSizeToFitWidth = YES,否则它会变得非常混乱,并且新尺寸将无法正常工作。

Everything seems to be broken in iOS 8 (probably iOS 7 too).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文