ios UILabel文本被截断

laximzn5  于 2023-04-08  发布在  iOS
关注(0)|答案(4)|浏览(204)

我正在尝试创建一个类似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应用程序的图像。第一句话从来没有得到切断在原来的Youtube应用程序。Youtube App
有没有什么方法可以像Youtube一样在我的应用程序中显示文本?

wbgh16ku

wbgh16ku1#

试着这样做:

label.lineBreakMode = .ByCharWrapping
label.numberOfLines = 0
wwtsj6pe

wwtsj6pe2#

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 .
ruyhziif

ruyhziif3#

您已硬修复标签的高度限制。请将其删除以获得动态高度。

xiozqbni

xiozqbni4#

如果您使用的是xcode,您可以从main.storyboard手动增加标签宽度

相关问题