【问题标题】:How to make a UILabel dynamic?如何使 UILabel 动态化?
【发布时间】:2019-07-06 21:22:59
【问题描述】:

如何根据给定的评论文本,根据需要多次制作 UIlabel Wrap?

我尝试了很多方法,例如:

        self.commentText.contentMode = .scaleToFill
    self.commentText.numberOfLines = 0
//        self.commentText.leadingMargin(pixel: 10)
//        self.commentText.trailingMargin(pixel: 10)

我从 [Qs like.] 和UILabel - auto-size label to fit text?1 得到的

但我无法使包装工作

我如何设置:

        private func setupTheCommentText() {
        self.commentText.frame = CGRect(x: 85, y: 35, width: 272, height: 15)
        self.commentText.textColor = UIColor(red: 92/255, green: 92/255, blue: 92/255, alpha: 1)
        self.commentText.font = UIFont(name: "SFCompactDisplay-Medium", size: 13)
        //self.commentText.numberOfLines = 0
        //self.commentText.contentMode = .scaleAspectFill
        //self.commentText.translatesAutoresizingMaskIntoConstraints = false
//        self.commentText.leadingMargin(pixel: 10)
//        self.commentText.trailingMargin(pixel: 10)
        self.addSubview(commentText)
        self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor).isActive = true
        commentText.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor),
            self.commentText.leadingAnchor.constraint(equalTo:self.contentView.leadingAnchor,constant:85.0),
            self.commentText.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor,constant:-15.0),//,constant:-85.0
            self.commentText.bottomAnchor.constraint(equalTo:commentText.bottomAnchor)
            ])
    }

【问题讨论】:

  • 你给标签固定宽度了吗?显示您用于将标签添加到视图的代码。或者显示故事板和约束的屏幕截图。
  • 从您的编辑中,您似乎混淆了使用框架和使用自动布局约束。您应该使用其中一种。您需要将其余约束添加到视图中。

标签: ios swift


【解决方案1】:

给出这些约束

leading,trailing,top,bottom // 底部用于单元格

commentText.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
  self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor),
  self.commentText.leadingAnchor.constraint(equalTo:self.view.leadingAnchor,constant:85.0),
  self.commentText.trailingAnchor.constraint(equalTo:self.view.trailingAnchor,constant:-85.0),
  self.commentText.bottomAnchor.constraint(equalTo:self.view.bottomAnchor) // this for cells 
])

上面我假设它在 vc 的视图中,如果不是这种情况,它是一个单元格,将 self.view 替换为 self.contentView

【讨论】:

  • 我已经用这段代码尝试了很多变体,虽然文本是换行的,但它现在看起来不是 x=85。如何让它从 85 开始?
  • ,constant:85.0 添加到leading/trailing 锚点查看编辑
  • 为了更接近预期的结果,我这样做了: NSLayoutConstraint.activate([ self.commentText.topAnchor.constraint(equalTo:username.bottomAnchor), self.commentText.leadingAnchor.constraint(equalTo:self .leadingAnchor,constant:85.0), self.commentText.trailingAnchor.constraint(equalTo:self.trailingAnchor),//,constant:-85.0 //self.commentText.bottomAnchor.constraint(equalTo:self.bottomAnchor) // this for细胞])
  • 问题是标签如何不换行
  • 表/集合在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
相关资源
最近更新 更多