2017-07-01 125 views
2

我正在尝试创建一个类似于Youtube的应用程序。我在这个项目中没有使用IB。我遇到的问题是,当句子太大而无法合并到一行时,文本会被切断。我想显示第一句话,而不是像Original Youtube应用程序那样被截断。下面给出了UILabel的代码。对于titlelabelUILabel被截断的文本

//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!) 

下面的图片可以给什么我上面的意思的想法

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

//约束。 My App

我也张贴原始Youtube应用程序的图像。第一句永远不会在原来的Youtube应用中被切断。 Youtube App

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

+1

尝试删除高度约束。它会自动扩展高度。 – xmhafiz

+0

谢谢你的工作。 –

+0

有一个小问题。当我删除高度约束时,第二条线与第一条线不一致,它稍微向右移动。你知道如何解决这个问题吗? –

回答

1

尝试这样做: -

label.lineBreakMode = .ByCharWrapping 
label.numberOfLines = 0 
+0

我试过你的,但它不显示整个句子,虽然第一行永远不会被切断 –

+0

尝试此除了删除高度限制。 –

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 . 
+0

我试过你,但它变得更糟,如果我添加底部约束,因为TitleLabel底部有另一个标签,它具有顶部约束,所以我不需要底部约束标题标签。 –