This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArticleClass { | |
let title: String | |
let url: URL | |
var readCount: Int = 0 | |
init(title: String, url: URL) { | |
self.title = title | |
self.url = url | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let text = dataSource.item(at: indexPath.item) | |
guard let nib = Bundle.main.loadNibNamed("CustomCell", owner: CustomCell.self, options: nil), let cell = nib?[0] as? CustomCell else { | |
return | |
} | |
// Ensure that your content has been set | |
cell.label.text = text | |
// Assuming your custom cell has a content view | |
cell.contentView.setNeedsLayout() | |
cell.contentView.layoutIfNeeded() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, heightForItemAt indexPath: IndexPath) -> CGFloat { | |
let text = dataSource.item(at: indexPath.item) | |
let font = Typography(for: .cellText).font() | |
return layout.textHeight(text, font: font) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, heightForItemAt indexPath: IndexPath) -> CGFloat { | |
let text = dataSource.item(at: indexPath.item) | |
let font = UIFont.preferredFont(forTextStyle: .body) | |
return layout.textHeight(text, font: font) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let layout = FlexibleRowHeightGridLayout() | |
layout.delegate = self | |
collectionView.collectionViewLayout = layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let layout = FlexibleRowHeightGridLayout() | |
layout.delegate = self | |
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { | |
setNeedsLayout() | |
layoutIfNeeded() | |
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size) | |
var newFrame = layoutAttributes.frame | |
// Make any additional adjustments to the cell's frame | |
newFrame.size = size | |
layoutAttributes.frame = newFrame | |
return layoutAttributes | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func awakeFromNib() { | |
super.awakeFromNib() | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
contentView.leftAnchor.constraint(equalTo: leftAnchor), | |
contentView.rightAnchor.constraint(equalTo: rightAnchor), | |
contentView.topAnchor.constraint(equalTo: topAnchor), | |
contentView.bottomAnchor.constraint(equalTo: bottomAnchor) | |
]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBOutlet weak var layout: UICollectionViewFlowLayout! { | |
didSet { | |
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { | |
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize | |
} |