Text not vertically centered in UILabel

前端 未结 2 1658
無奈伤痛
無奈伤痛 2021-01-31 13:48

I\'ve created a Label with the following code :

func setupValueLabel() {
    valueLabel.numberOfLines = 1
    valueLabel.font = UIFont(name: \"Avenir-Black\", s         


        
2条回答
  •  伪装坚强ぢ
    2021-01-31 14:31

    The problem is that font size is shrunk by adjustsFontSizeToFitWidth = true, but it does not adjust the lineHeight automatically. It remains to be for original font size that is 50.

    By default, the text is aligned to its baseline. you can adjust it with baselineAdjustment property.

    In your case, you should set it to UIBaselineAdjustment.alignCenters.

    valueLabel.baselineAdjustment = .alignCenters
    

提交回复
热议问题