'UITextView cutting off last line of text
I am trying to render out a UITextView
within a UITableViewCell
.
For some reason the last line of my text is being cut off. I have followed numerous posts on here and none of seemed to work.
As you can see the dummy text I have added has additional text now displayed.
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),
])
}
}
EDIT
My tableview uses
tableView.register(CardCell.self, forCellReuseIdentifier: cellID)
tableView.allowsSelection = false
tableView.separatorStyle = .none
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 240
Solution 1:[1]
Try this cheating
let tv = UITextView(frame: .zero)
tv. ...
tv.text = "Lorem ipsum..."
tv.isScrollEnabled = true
tv.isScrollEnabled = false
return tv
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Silver |