简体   繁体   中英

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:

在此处输入图片说明

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 :)

将具有适当大小的宽度锚点添加到 oderLabel 可能会奏效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM