UILabel正在切断文本

时间:2017-07-01 17:06:27

标签: ios swift

我正在尝试创建一个像应用程序的Youtube。我没有在这个项目中使用IB。我遇到的问题是,当句子太大而不适合一行时,文本会被切断。我想显示第一句而不是像Original Youtube应用程序一样被切断。 UILabel的代码如下所示。

let titleLabel:UILabel = {
        let label = UILabel()
        label.text = "Linkin Park - Numb"
        label.translatesAutoresizingMaskIntoConstraints = false
        label.numberOfLines = 2
        return label
    }()

// titlelabel的约束

    //top
addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top,       relatedBy: .Equal, toItem: thumbnailImageView, attribute: .Bottom, multiplier: 1, constant: 8))

    //left
  addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Left, relatedBy: .Equal, toItem: profileImageView, attribute: .Right, multiplier: 1, constant: 8))
    //right
addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Right, relatedBy: .Equal, toItem: thumbnailImageView, attribute: .Right, multiplier: 1, constant: 0))
    // height
titleLabelHeightConstraint = NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 0, constant: 44)
        addConstraint(titleLabelHeightConstraint!)

下面的图片可以说明我的意思。 My App

我还发布了原始Youtube App的图片。第一句话永远不会被原始Youtube应用程序中断。 Youtube App

有什么方法可以像我的应用程序中的Youtube一样显示文字吗?

3 个答案:

答案 0 :(得分:1)

尝试这样做: -

label.lineBreakMode = .ByCharWrapping
label.numberOfLines = 0

答案 1 :(得分:1)

Hi just remove the height constraint 
titleLabelHeightConstraint = NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 0, constant: 44)
        addConstraint(titleLabelHeightConstraint!)

and **add the bottom constraint to the label** as in current scenario the label does't have the bottom constraint so that label does't know how much increase .

答案 2 :(得分:0)

您对标签的高度约束进行了硬固定。请删除它以获取动态高度。

相关问题