Skip to content

Instantly share code, notes, and snippets.

@jolasjoe
Last active March 14, 2022 15:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jolasjoe/652dad8223808709ba2bf5ccda5b1b9c to your computer and use it in GitHub Desktop.
Save jolasjoe/652dad8223808709ba2bf5ccda5b1b9c to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// CVSelfSizingCells
//
// Created by Jolas L on 13/06/20.
// Copyright © 2020 Jolas L. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource {
let collectionView: UICollectionView = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.backgroundColor = .white
collectionView.register(MultiLineCell.self, forCellWithReuseIdentifier: "cellId")
NSLayoutConstraint.activate([
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
dummyTextMessages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! MultiLineCell
cell.textLabel.text = dummyTextMessages[indexPath.row]
cell.maxWidth = collectionView.frame.width
return cell
}
}
class MultiLineCell: UICollectionViewCell{
let containerView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let textLabel: UILabel = {
let textLabel = UILabel()
textLabel.backgroundColor = .red
textLabel.font = UIFont.systemFont(ofSize: 20)
textLabel.numberOfLines = 0
textLabel.translatesAutoresizingMaskIntoConstraints = false
return textLabel
}()
private var containerViewWidthAnchor:NSLayoutConstraint!
private var textViewHeightContraint: NSLayoutConstraint!
var maxWidth: CGFloat? {
didSet {
guard let maxWidth = maxWidth else {
return
}
containerViewWidthAnchor.constant = maxWidth
containerViewWidthAnchor.isActive = true
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .cyan
contentView.addSubview(containerView)
containerView.addSubview(textLabel)
containerViewWidthAnchor = containerView.widthAnchor.constraint(equalToConstant: 0)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
containerView.topAnchor.constraint(equalTo: contentView.topAnchor),
containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
textLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
textLabel.topAnchor.constraint(equalTo: containerView.topAnchor),
textLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
textLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let dummyTextMessages = [
"Hello",
"Hi",
"Hey! Last time I have purchased a brand new watch from solutions.",
"Thanks for contacting us we are here to help you. Please read all the schemes. ",
"Sathish Kumar V Thanks for reaching out. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambprinter took a galley Hgjhagsdg asjhdgjhasgdjha asjhdgajhsdghjasg asjhdgjhagsd jhagsdjhgasjhdgajsd",
"Hel asdjjha skjhajsdhalo",
"H jkahskdhaksdh jkahsgdjh jahsgh hgashdgh hhh hghg hghsgdhgash ello",
"kjjkasjkdjk jaskjdjkasdjk askjjkaskjkjkjskj nskjkskik s k skksknn",
"aslkdjkjas ajksdkjasd ajksdkjasd",
"askjdjkasdjkasd",
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment