Home > database >  Splitting a string truncates the word in UILabel
Splitting a string truncates the word in UILabel

Time:12-22

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:

1

But if I try set title for example "Развлечения", UILabel truncates the word.

One word is split into two lines:

2

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

CodePudding user response:

The problem is, as said in the comments, that characters don't necessarily have the same width.

  • lllll
  • AAAAA
  • aaaaa

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.

  • Related