I try to setup a custom button with UIImage
and UILabel
After setting up constraints, I started testing this button and noticed strange behavior
UILabel
in UIButton
code:
private var title: UILabel = {
var label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.boldSystemFont(ofSize: 14)
label.numberOfLines = 0
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
return label
}()
When I set title for example "Hfpdktxtybz"
, UILabel
works amazing!
One word takes one line:
But if I try set title for example "Развлечения"
, UILabel
truncates the word.
One word is split into two lines:
Why for English language label work is correctly, but for Russian language truncates the word? How to fix it?
The number of characters is the same
2
Answers
The problem is, as said in the comments, that characters don’t necessarily have the same width.
The above example clearly shows characters do not have the same width although they have same character count.
So Autolayout calculates the UILabel real width and it has only one option in order to satisfy your constraints. That is to split it into 2 lines.
If you don’t want this to happen consider changing UILabel priority
numberOfLines
.if you use storyboard
if you use swift programmatically.