1
votes

I try to figure out autoshrink option in UILabel with regarding top constraint.

I have UIView and three labels. One of them has autoshrink option on. It has constraint to be centered, and has Trailing and Top constraint which should shrink label when changing size of UIView. If I make UIVIew thinner, font size is decreased, but if I change height of UIView font is not changed.

Constraints on UILabels :

  • Align Center X to Superview
  • Align Center Y to Superview
  • Trailing Space to Superview >= 50
  • Top Space to Superview >= 40
  • Align Center X to label2
  • Top Space to label1 equals :15
  • Bottom space to label2 equals :3

Label 1 constraints :

  • Align Center x to superview
  • Trailing Space to superview >=10
  • Leading Space to superview >=10
  • Bottom Space to Shrink Label equal 15

Label 2 constraint :

  • Align Center X to Shrink label
  • Top Space to Shrink label equals 3

How to change this?

What I want is, on last image that label will be nice autoshrink. So if I change width or height of the UIView label should shrink.

Normal size

Width change

Height change

2
Can you explain it more . - balkaran singh
sure, what do you need to know. - Marko Zadravec
are you want shrink label change according to view ? - balkaran singh
Yes. If you look last image you can see that shrink is not what I want - Marko Zadravec
ok i got it plz wait for answer. - balkaran singh

2 Answers

2
votes

plz select your Shrink label set

Number of lines is 0

Line Breaks: Clip

Autoshrink: Minimum Font Scale 0.25 enter image description here

1
votes

I hope below code will help you in some way,

extension Double {
    /// Returns propotional width according to device width
    var propotional: CGFloat {
        if UIDevice.current.userInterfaceIdiom == .pad {
            return CGFloat(414.0) / CGFloat(375.0) * CGFloat(self)
        }
        return CGFloat(Screen.width) / CGFloat(375.0) * CGFloat(self)
    }
}



extension UILabel {
        /// This property is change font-size of Label's text propotionaly, if assigned `On` from Interface builder
       @IBInspectable
        var isPropotional: Bool{
            get {
                return true
            }

        set {
                if newValue == true {
                    let fontSize = Double((self.font!.pointSize))
                    self.font = UIFont(name: self.font!.fontName, size: fontSize.propotional)
            }
        }
    }
}

In extension of Double, propotional value is calculated by considering current device set in storyboard as iPhone 6 size.

enter image description here
After adding this code set on from interface builder for Label where you can see isPropotional attribute in attribute inspector tab. Please refer image.