Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jolasjoe/d2fd89cbd8e8e213ed3c280f08a08f0b to your computer and use it in GitHub Desktop.
Save jolasjoe/d2fd89cbd8e8e213ed3c280f08a08f0b 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.textView.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 textView: UITextView = {
let textView = UITextView()
textView.backgroundColor = .red
textView.font = UIFont.systemFont(ofSize: 20)
textView.isScrollEnabled = false
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
private var containerViewWidthAnchor:NSLayoutConstraint!
private var textViewHeightContraint: NSLayoutConstraint!
var maxWidth: CGFloat? {
didSet {
guard let maxWidth = maxWidth else {
return
}
containerViewWidthAnchor.constant = maxWidth
containerViewWidthAnchor.isActive = true
let sizeToFitIn = CGSize(width: maxWidth, height: CGFloat(MAXFLOAT))
let newSize = self.textView.sizeThatFits(sizeToFitIn)
self.textViewHeightContraint.constant = newSize.height
}
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .cyan
contentView.addSubview(containerView)
containerView.addSubview(textView)
textViewHeightContraint = textView.heightAnchor.constraint(equalToConstant: 0)
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),
textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
textView.topAnchor.constraint(equalTo: containerView.topAnchor),
textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
textView.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",
]
@junsukim1994
Copy link

This piece of code does not work in Swift 5, Xcode 12.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment