Search code examples
iosswiftconstraintsuilabel

Swift - UILabel text not displayed correctly


I have no idea why my UILabel is not being displayed correctly but instead Swift shortens it like you can see in the picture below:

enter image description here

This is how I create the label and both of the lines:

let oderLabel: UILabel = {
    let v = UILabel()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.font = UIFont(name: "AvenirNext-DemiBold", size: 15)
    v.textColor = .white
    v.textAlignment = .center
    v.text = "ODER"
    return v
}()

let lineLeft: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.image = UIImage(named: "line")
    return v
}()

let lineRight: UIImageView = {
    let v = UIImageView()
    v.translatesAutoresizingMaskIntoConstraints = false
    v.image = UIImage(named: "line")
    return v
}()

And my constraints:

    oderLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    oderLabel.bottomAnchor.constraint(equalTo: weiterButton.bottomAnchor, constant: 40).isActive = true
    
    lineLeft.centerYAnchor.constraint(equalTo: oderLabel.centerYAnchor).isActive = true
    lineLeft.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
    lineLeft.trailingAnchor.constraint(equalTo: oderLabel.leadingAnchor).isActive = true
    
    lineRight.centerYAnchor.constraint(equalTo: oderLabel.centerYAnchor).isActive = true
    lineRight.leadingAnchor.constraint(equalTo: oderLabel.trailingAnchor).isActive = true
    lineRight.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true

All I want is to center the label and have both of the lines with little bit of space next to it. And that should be correctly displayed on all iPhone sizes. I am on this now for way too long..

It should be a 1 min task so I probably have some misunderstanding. If anyone could help me out here I'd really appreciate it :)


Solution

  • Adding a width anchor with an appropriate size to oderLabel might do the trick.