的UILabel不破线的UITableViewCell

问题描述 投票:2回答:3

我在建设有一样的界面,一个信使的应用程序。 我使用的tableView做到这一点。每个单元包含一个UIView - 消息气泡和UILabel - 即嵌套在UIView该消息。 它当UILabel应该打破它没有线,这一切是在一个行上小尺寸的文本,但由于某些原因的伟大工程。线的量被设定为零。

这是我的信息处理类:

    func commonInit() {

    print(MessageView.frame.height)
    MessageView.clipsToBounds = true
    MessageView.layer.cornerRadius = 15
    myCellLabel.numberOfLines = 0
    let bubbleSize = CGSize(width: self.myCellLabel.frame.width + 28, height: self.myCellLabel.frame.height + 20)
    print(bubbleSize.height)
    MessageView.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: bubbleSize.width, height: bubbleSize.height)


    if reuseIdentifier! == "Request" {
        MessageView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMinXMaxYCorner]
        MessageView.backgroundColor = UIColor(red: 0, green: 122/255, blue: 1.0, alpha: 1.0)
    } else  {
        MessageView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner, .layerMaxXMaxYCorner]
        MessageView.backgroundColor = UIColor.lightGray
    }


}

小区调用函数:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if queryCounter % 2 == 0 && indexPath.row % 2 == 0{
        cellReuseIdentifier = "Answer"

    } else {
        cellReuseIdentifier = "Request"
    }

    let cell:MessageCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MessageCell
    cell.myCellLabel.textColor = UIColor.white
    cell.myCellLabel.text = self.messages[indexPath.row]
    let height = cell.myCellLabel.text!.height(withConstrainedWidth: cell.myCellLabel.frame.width, font: cell.myCellLabel.font)
    print(height)
    cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1)

    return cell
}

高度可变的基于文本的大小来计算。这表明,文本大小正常计算 - 占换行符。我在此基础上计算无法修改单元格高度 - 什么我试过的作品。我想这可能是一个制约的问题。 我的约束:enter image description here

enter image description here

如何让我断行?请帮忙。

编辑:我刚刚注意到MessageView.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: bubbleSize.width, height: bubbleSize.height)没有影响使以往任何时候对信息泡泡什么。

ios swift uitableview uilabel
3个回答
1
投票

在使用自动布局将无法正常工作设定框架。

我不能说究竟是什么在这里发生而不需要将整个情境,但重复使用电池和自动布局时,一些常见的陷阱是:

  1. 忘记设置自动高度为你的细胞(在故事板扩展蜂窝手动将覆盖此设置)
  2. 泰伯维也需要estimatedHeight有时能正常工作
  3. 有时候,你需要调用setNeedsLayout内容添加到小区后,

检查控制台,如果有关于违约的约束一些警告,你可以很容易地找到问题的存在。


1
投票

尝试找到基于标签宽度和文本字体标签的高度,然后设置标签高度约束了这一点。

extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)

        return ceil(boundingBox.height)
    }
}

是这样的:

let textHeight = yourtext.height(withConstrainedWidth: yourlabel.frame.width, font: font)
yourLabel.heightAnchor.constraint(equalToConstant: textHeight).isActive = true

1
投票

多个小时的努力后,一切都我设法解决它。问题是与约束。 1.正如你可以看到在这个古老的布局。 UIView的到处受限但左 - >这就是文云。 enter image description here

  1. 任何文本被初始化之前被称为的UITableViewCell的commonInit()方法。这不是一件好事,因为所有的细胞大小调整是基于其尚未传递给单元格文本 - >移动单元初始化后的方法。 let cell:MessageCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MessageCell cell.myCellLabel.text = self.messages[indexPath.row] //Before calling commonInit() we need to adjust the cell height. let height = cell.myCellLabel.text!.heightForView(text: cell.myCellLabel.text!, font: cell.myCellLabel.font, width: self.view.frame.width / 2) // Then we set the width of the UILabel for it to break lines at 26 characters if cell.myCellLabel.text!.count > 25 { tableView.rowHeight = height + 20 cell.myCellLabel.widthAnchor.constraint(equalToConstant: cell.frame.width / 2).isActive = true cell.updateConstraints() } // Calling commonInit() after adjustments cell.commonInit() cell.contentView.transform = CGAffineTransform(scaleX: 1, y: -1) return cell
  2. 然后,我们需要更新约束,这样的的UIView和的UILabel与细胞高度调整。 enter image description here

enter image description here

  1. 完成。现在,它的工作原理是必要的。感谢所有的建议! enter image description here
© www.soinside.com 2019 - 2024. All rights reserved.