WikipediaBrown
WikipediaBrown

Reputation: 817

How do I offset UILabel text alignment?

I am trying to offset the center starting point for a UILabel. The problem that I am facing is that I can't make the label text grow from a point that is offset from center until it reaches one end of the label. Then, I want the text to shift one character to the left with each additional added character until it reaches the the other end. Then it would be acceptable for the text to truncate with an ellipses.

So far, my code looks like this but I don't know where to go from here.

    private let amountLabel: UILabel = {
        let label = UILabel()
        label.textColor = .blue
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

enter image description here

Upvotes: -1

Views: 1326

Answers (1)

Au Ris
Au Ris

Reputation: 4659

I suggest you put the UILabel into a container view, center horizontally with the offset you want, but set its priority lower than the compression resistance., e.g. 750, compression resistance 999. Then create a trailing constraint >= with priority 1000, and leading constraint >= priority 1000. In that way centering will be the weakest constraint and as the text grows it will shift to the left until it reaches the leading.

enter image description here

enter image description here enter image description here enter image description here

Upvotes: 2

Related Questions