0

I am trying to get my itemLabel UILabel to be multiline and do word wrapping, but I can't figure out where I'm going wrong here. I have numberOfLines = 0 and .byWordWrapping set. I'm sure it's a simple line of code I messed up, but I can't find it.

import UIKit
import Foundation

protocol ListItemCellDelegate {
    func setChecked(cell: ListItemCell)
}

class ListItemCell: UITableViewCell {
    var delegate: ListItemCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }

    let cellView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.setCellShadow()
        return view
    }()

    let checkButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "UnChecked"), for: .normal)
        button.setImage(UIImage(named: "Checked"), for: .selected)
        button.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        return button
    }()

    let priorityButton: UIButton = {
        let button = UIButton(type: .custom)
        button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = UIColor.white
        button.layer.borderWidth = 2
        button.layer.borderColor = UIColor.black.cgColor
        button.titleLabel?.font = UIFont.init(name: "Avenir Next", size: 24)
        return button
    }()

    let itemLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = UIFont.init(name: "Avenir Next", size: 16)
        return label
    }()

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

        checkButton.addTarget(self, action: #selector(setChecked), for: .touchUpInside)
        itemLabel.translatesAutoresizingMaskIntoConstraints = false
        itemLabel.numberOfLines = 0
        itemLabel.lineBreakMode = .byWordWrapping
    }

    func setupCell() {
        addSubview(cellView)
        cellView.addSubview(checkButton)
        cellView.addSubview(priorityButton)
        cellView.addSubview(itemLabel)

        cellView.setAnchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8)
        checkButton.setAnchor(top: nil, left: cellView.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 50, height: 50)
        checkButton.centerYAnchor.constraint(equalTo: cellView.centerYAnchor).isActive = true
        priorityButton.setAnchor(top: nil, left: checkButton.rightAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
        priorityButton.centerYAnchor.constraint(equalTo: checkButton.centerYAnchor).isActive = true
        itemLabel.setAnchor(top: nil, left: priorityButton.rightAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 20)
        itemLabel.centerYAnchor.constraint(equalTo: priorityButton.centerYAnchor).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func setChecked() {
        self.delegate?.setChecked(cell: self)
    }
}

I expect the UILabel to wrap the text and take up as much space as needed to fit the whole label text in the cell. Right now it is not displaying the entire message, only what the size will allow. But, I want it to resize based on the length of the string. I did this easily in Storyboard, but I'm trying to get it to work programmatically.

Vaisakh KP
  • 467
  • 1
  • 6
  • 25

2 Answers2

1

I have done that before like in UITableView() there was an array of Strings and the length of the String was not fixed it can be in 3 lines or more.

So for that, i have given the constraints to UILabel() like top, bottom,leading, trailing all constraint to 0.

In UITableView(), there is a method called :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Try this one it will definitly help you i guess.

  • That was part of the solution. The other part was setting heightAnchor to >= 60 as that's what I'm using for my minimum size. Thanks for the suggestion! – Timothy Waters Feb 03 '19 at 20:50
0

In my case, part of the solution was what Varsha Shivhare suggested, and the other was setting the heightAnchor constraint to >= 60 which is what I'm using right now. Thank you!