UITextView切断文本的最后一行

时间:2019-06-14 09:55:55

标签: swift uitableview uikit uitextview

我正在尝试在UITextView中渲染UITableViewCell

由于某种原因,我的文字的最后一行被截断了。我在这里关注了无数帖子,似乎都没有用。

您会看到我添加的虚拟文本现在显示了其他文本。

import UIKit

class CardCell: UITableViewCell {

    private lazy var featureImage: UIImageView = {
        let iv = UIImageView(frame: .zero)
        iv.translatesAutoresizingMaskIntoConstraints = false
        iv.contentMode = .scaleAspectFill
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 2
        return iv
    }()

    private lazy var titleLabel: UILabel = {
        let l = UILabel(frame: .zero)
        l.translatesAutoresizingMaskIntoConstraints = false
        l.font = .systemFont(ofSize: 16, weight: .medium)
        l.textColor = .black
        return l
    }()

    private lazy var infoLabel: UITextView = {
        let tv = UITextView(frame: .zero)
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.textContainerInset = .init(top: 0, left: -4, bottom: 0, right: 0)
        tv.font = .systemFont(ofSize: 12, weight: .light)
        tv.textColor = .black
        tv.isEditable = false
        tv.isSelectable = false
        tv.isScrollEnabled = false
        tv.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer consectetur felis ut nulla molestie maximus. Etiam scelerisque nibh tellus, eu hendrerit magna iaculis at."
        tv.sizeToFit()
        return tv
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.backgroundColor = .white
        backgroundColor = .clear

    }

    required init?(coder: NSCoder) {
        return nil
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        contentView.frame = contentView.frame.inset(by: .init(top: 10, left: 12, bottom: 10, right: 12))
    }

    func render(_ model: CellData) {

        featureImage.image = model.featureImage
        titleLabel.text = model.title

        configureConstraints()

    }

    func configureConstraints() {
        [featureImage, titleLabel, infoLabel].forEach { contentView.addSubview($0) }

        NSLayoutConstraint.activate([
            featureImage.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
            featureImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            featureImage.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
            featureImage.heightAnchor.constraint(equalToConstant: 140),

            titleLabel.topAnchor.constraint(equalTo: featureImage.bottomAnchor, constant: 8),
            titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),

            infoLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 0),
            infoLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8),
            infoLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
            infoLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8),
        ])
    }
}

cell output

编辑

我的表视图使用

tableView.register(CardCell.self, forCellReuseIdentifier: cellID)
tableView.allowsSelection = false
tableView.separatorStyle = .none
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 240

0 个答案:

没有答案