Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the same font size for different labels AFTER scaling

I am making an app where I have 3 labels. I am using label auto-shrinking to help adapt the label's font size to the device.

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.

Thanks.

like image 896
D-A UK Avatar asked Mar 27 '18 17:03

D-A UK


People also ask

How do I change font size in labels?

To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size.

What is dynamic type font?

The Dynamic Type feature allows users to choose the size of textual content displayed on the screen. It helps users who need larger text for better readability. It also accomodates those who can read smaller text, allowing more information to appear on the screen.

What is font scaling?

1. Font scaling is an alternate term used to describe a scalable font or vector font. 2. Font scaling is a printer feature that allows the font to be scaled by the printer instead of in the document program.

How do I change font size in labels in Visual Basic?

Instead just go to the properties of whatever you're trying to change the font size of, and go to the font property. Click on the 3 dots (...) and a box will open. In that box, you can change the font and its size too.


1 Answers

Programatically change UIlabel font size after dynamic resizing. 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

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

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

enter image description here

Output

enter image description here

like image 135
Britto Thomas Avatar answered Nov 15 '22 12:11

Britto Thomas