我不知道为什么我的UILabel无法正确显示,但是Swift缩短了它,就像您在下图中看到的那样:

ios - Swift-UILabel文字无法正确显示-LMLPHP

这就是我创建label和两个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
}()


而我的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


我想要的只是将label居中,并在两个lines旁边都留有一点空间。而且应该在所有iPhone尺寸上正确显示。我现在在这上的时间太长了。

这应该是1分钟的任务,所以我可能会有一些误会。如果有人可以在这里帮助我,我将非常感激:)

最佳答案

向oderLabel添加适当大小的宽度锚点可能会成功。

关于ios - Swift-UILabel文字无法正确显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59866229/

10-13 05:03