简体   繁体   English

缩放后为不同的标签设置相同的字体大小

[英]Setting the same font size for different labels AFTER scaling

I am making an app where I have 3 labels.我正在制作一个有 3 个标签的应用程序。 I am using label auto-shrinking to help adapt the label's font size to the device.我正在使用 label 自动缩小来帮助使标签的字体大小适应设备。

These labels are right next to each other, and that therefore means that I want them to have them the same font size.这些标签彼此相邻,因此这意味着我希望它们具有相同的字体大小。 What currently happens is (because they have different amounts of text) they end up shrinking to different font sizes.目前发生的情况是(因为它们有不同数量的文本)它们最终缩小到不同的字体大小。

Is there a way to make it so that after scaling, the label with the smallest font size is the standard font for all of the other labels.有没有办法让它在缩放后,最小字体的 label 是所有其他标签的标准字体。

Thanks.谢谢。

Programatically change UIlabel font size after dynamic resizing. 动态调整大小后以编程方式更改UIlabel字体大小。 See the example below. 请参阅下面的示例。 Calculate current font size with length of string & font. 使用字符串和字体的长度计算当前字体大小。 And then get minimum font size and apply separately for each UILabel 然后获得最小字体大小并单独申请每个UILabel

override func viewWillAppear(_ animated: Bool) {
    let fontSize1 = self.label1.getFontSizeForLabel()
    let fontSize2 = self.label2.getFontSizeForLabel()
    let fontSize3 = self.label3.getFontSizeForLabel()

    let smallestFontSize = min(min(fontSize1, fontSize2), fontSize3)

    self.label1.font = self.label1.font.withSize(smallestFontSize)
    self.label2.font = self.label2.font.withSize(smallestFontSize)
    self.label3.font = self.label3.font.withSize(smallestFontSize)

    self.label1.adjustsFontSizeToFitWidth = false
    self.label2.adjustsFontSizeToFitWidth = false
    self.label3.adjustsFontSizeToFitWidth = false
}

UILabel Extension UILabel扩展

extension UILabel {
    func getFontSizeForLabel() -> CGFloat {
        let text: NSMutableAttributedString = NSMutableAttributedString(attributedString: self.attributedText!)
        text.setAttributes([NSAttributedStringKey.font: self.font], range: NSMakeRange(0, text.length))
        let context: NSStringDrawingContext = NSStringDrawingContext()
        context.minimumScaleFactor = self.minimumScaleFactor
        text.boundingRect(with: self.frame.size, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: context)
        let adjustedFontSize: CGFloat = self.font.pointSize * context.actualScaleFactor
        return adjustedFontSize
    }
}

Storyboard 故事板

在此输入图像描述

Output 产量

在此输入图像描述

To get the smallest font size, you can use the compactMap .要获得最小的字体大小,您可以使用compactMap Then, you can use the min function to find the minimum font size in the array.然后,您可以使用 min function 来查找数组中的最小字体大小。

Here's an example of how you could do this:这是您如何执行此操作的示例:

        let labels = mainContainer
            .arrangedSubviews
            .compactMap { $0 as? UILabel }

        let newFontsSize = labels.map { label in
            var fontSize = label.font.pointSize
            
            while label.isTruncated {
                fontSize -= 0.25
                label.font = label.font.withSize(fontSize)
            }

            return fontSize
        }

        if let smallestFontSize = newFontsSize.min() {
            labels.forEach { label in
                label.font = label.font.withSize(smallestFontSize)
            }
        }

And the extension:和扩展名:

extension UILabel {
    var isTruncated: Bool {
        frame.width < intrinsicContentSize.width
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM