当 -adjustsFontSizeToFitWidth 设置为 YES 时,如何计算 UILabel 的字体大小?

发布于 2024-08-23 23:19:05 字数 288 浏览 6 评论 0原文

myLabel.adjustsFontSizeToFitWidth = YES时,UILabel将自动调整字体大小,以防文本对于标签来说太长。例如,如果我的标签只有 100px 宽,并且我的文本太长,无法适应当前的字体大小,它会缩小字体大小,直到文本适合标签。

当字体大小缩小时,我需要从 UILabel 获取实际显示的字体大小。例如,假设我的字体大小实际上是 20,但 UILabel 必须将其缩小到 10。当我向 UILabel 询问字体和字体大小时,我得到的是旧字体大小 (20),但不是显示的字体大小(10)。

When myLabel.adjustsFontSizeToFitWidth = YES, UILabel will adjust the font size automatically in case the text is too long for the label. For example, if my label is just 100px wide, and my text is too long to fit with the current font size, it will shrink down the font size until the text fits into the label.

I need to get the actual displayed font size from UILabel when the font size got shrunk down. For example, let's say my font size was actually 20, but UILabel had to shrink it down to 10. When I ask UILabel for the font and the font size, I get my old font size (20), but not the one that's displayed (10).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

半夏半凉 2024-08-30 23:19:05

我不确定这是否完全准确,但希望它应该非常接近。它可能不会考虑截断的字符串或标签的高度,但您可以手动执行此操作。

该方法

- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSizeactualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)宽度 lineBreakMode:(UILineBreakMode)lineBreakMode

将返回文本大小,并注意它还有一个实际使用的字体大小的参考参数。

I'm not sure if this is entirely accurate, but it should be pretty close, hopefully. It may not take truncated strings into account, or the height of the label, but that's something you might be able to do manually.

The method

- (CGSize)sizeWithFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

will return the text size, and notice that it also has a reference parameter for the actual font size used.

秋叶绚丽 2024-08-30 23:19:05

万一有人仍然需要答案。
在 iOS9 中,您可以使用 boundingRectWithSize:options:context: 来计算实际字体大小。请注意,context.minimumScaleFactor 不应为 0.0 才能进行缩放。

- (CGFloat)adjustedFontSizeForLabel:(UILabel *)label {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
    [text setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, text.length)];

    NSStringDrawingContext *context = [NSStringDrawingContext new];
    context.minimumScaleFactor = label.minimumScaleFactor;
    [text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
    CGFloat adjustedFontSize = label.font.pointSize * context.actualScaleFactor;

    return adjustedFontSize;
}

In case anybody still needs the answer.
In iOS9 you can use boundingRectWithSize:options:context: to calculate actual font size. Note that context.minimumScaleFactor should not be 0.0 for scaling to work.

- (CGFloat)adjustedFontSizeForLabel:(UILabel *)label {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:label.attributedText];
    [text setAttributes:@{NSFontAttributeName:label.font} range:NSMakeRange(0, text.length)];

    NSStringDrawingContext *context = [NSStringDrawingContext new];
    context.minimumScaleFactor = label.minimumScaleFactor;
    [text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin context:context];
    CGFloat adjustedFontSize = label.font.pointSize * context.actualScaleFactor;

    return adjustedFontSize;
}
久伴你 2024-08-30 23:19:05

对于单行 UILabel,这个简单的解决方案可以很好地工作:

//myLabel - initial label

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = myLabel.font;
fullSizeLabel.text = myLabel.text;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);

//correct, if new font size bigger than initial
actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;

For one-line UILabel works fine this simple solution:

//myLabel - initial label

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = myLabel.font;
fullSizeLabel.text = myLabel.text;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = myLabel.font.pointSize * (myLabel.bounds.size.width / fullSizeLabel.bounds.size.width);

//correct, if new font size bigger than initial
actualFontSize = actualFontSize < myLabel.font.pointSize ? actualFontSize : myLabel.font.pointSize;
遗弃M 2024-08-30 23:19:05

Swift 5

对于单行 UILabel

extension UILabel {

    var actualFontSize: CGFloat {
    //initial label
     let fullSizeLabel = UILabel()
     fullSizeLabel.font = self.font
     fullSizeLabel.text = self.text
     fullSizeLabel.sizeToFit()

     var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);

    //correct, if new font size bigger than initial
     actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;

     return actualFontSize
    }

}

获取实际字体大小非常简单:

let currentLabelFontSize = myLabel.actualFontSize

Swift 5

For one-line UILabel

extension UILabel {

    var actualFontSize: CGFloat {
    //initial label
     let fullSizeLabel = UILabel()
     fullSizeLabel.font = self.font
     fullSizeLabel.text = self.text
     fullSizeLabel.sizeToFit()

     var actualFontSize: CGFloat = self.font.pointSize * (self.bounds.size.width / fullSizeLabel.bounds.size.width);

    //correct, if new font size bigger than initial
     actualFontSize = actualFontSize < self.font.pointSize ? actualFontSize : self.font.pointSize;

     return actualFontSize
    }

}

Getting the actual font size is then as simple as:

let currentLabelFontSize = myLabel.actualFontSize
与他有关 2024-08-30 23:19:05
UILabel *txtLabel = [[UILabel alloc] initWithFrame:rectMax];
txtLabel.numberOfLines = 1;
txtLabel.font = self.fontMax;
txtLabel.adjustsFontSizeToFitWidth = YES;
txtLabel.minimumScaleFactor = 0.1;
[txtLabel setText:strMax];

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = txtLabel.font;
fullSizeLabel.text = txtLabel.text;
fullSizeLabel.numberOfLines = 1;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = txtLabel.font.pointSize * (txtLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
actualFontSize = actualFontSize < txtLabel.font.pointSize ? actualFontSize : txtLabel.font.pointSize;
// the actual font
self.fontMax = [UIFont fontWithName:self.fontMax.fontName size:actualFontSize];

我的代码工作得很好,部分来自@Igor

UILabel *txtLabel = [[UILabel alloc] initWithFrame:rectMax];
txtLabel.numberOfLines = 1;
txtLabel.font = self.fontMax;
txtLabel.adjustsFontSizeToFitWidth = YES;
txtLabel.minimumScaleFactor = 0.1;
[txtLabel setText:strMax];

UILabel *fullSizeLabel = [UILabel new];
fullSizeLabel.font = txtLabel.font;
fullSizeLabel.text = txtLabel.text;
fullSizeLabel.numberOfLines = 1;
[fullSizeLabel sizeToFit];

CGFloat actualFontSize = txtLabel.font.pointSize * (txtLabel.bounds.size.width / fullSizeLabel.bounds.size.width);
actualFontSize = actualFontSize < txtLabel.font.pointSize ? actualFontSize : txtLabel.font.pointSize;
// the actual font
self.fontMax = [UIFont fontWithName:self.fontMax.fontName size:actualFontSize];

my code works great, part from @Igor

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