I am creating a sample app which has some texts on UIButton
and also image
, for image
and text
are subView
of UIButton
. I have run with different devices like iPhone 5
, iPhone X
and the result not like what i expected because if run with device which its screen smaller than iPhone X
then the text will be floating outside the button.
What i have done so far:
lazy var myLabelButton: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "titleName"
label.textColor = .white
label.font = UIFont(name: "SFCompactText-Regular", size: 14)
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.sizeToFit()
return label
}()
I searched for some resources and they let me apply these functions:
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.sizeToFit()
Those above three functions are not work
Here is the whole code:
button.addSubview(myLabelButton)
NSLayoutConstraint.activate([
myLabelButton.centerXAnchor.constraint(button.centerXAnchor),
myLabelButton.centerYAnchor.constraint(button.centerYAnchor),
myLabelButton.widthAnchor.constraint(button.widthAnchor)
])
Any solution for this?