From 2454236fd6d1c7492742a2979836666d07930bd0 Mon Sep 17 00:00:00 2001 From: Khrystyna Shevchuk Date: Fri, 7 Jul 2017 18:10:56 +0300 Subject: [PATCH] - added PinterestCell to PinterestLayout - added setting cell programmatically to PinterestLayout --- PinterestLayout.xcodeproj/project.pbxproj | 12 ++ PinterestLayout/PinterestCell.swift | 188 ++++++++++++++++++++++ PinterestLayout/UIView+Constraints.swift | 62 +++++++ PinterestLayoutExample/PinterestVC.swift | 22 ++- 4 files changed, 276 insertions(+), 8 deletions(-) create mode 100644 PinterestLayout/PinterestCell.swift create mode 100644 PinterestLayout/UIView+Constraints.swift diff --git a/PinterestLayout.xcodeproj/project.pbxproj b/PinterestLayout.xcodeproj/project.pbxproj index 7f851a0..9f450eb 100644 --- a/PinterestLayout.xcodeproj/project.pbxproj +++ b/PinterestLayout.xcodeproj/project.pbxproj @@ -9,6 +9,9 @@ /* Begin PBXBuildFile section */ 640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640095F61F0BC37F006AE391 /* CollectionViewCell.swift */; }; 644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */; }; + 646640361F0F9E5F0076207D /* PinterCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646640351F0F9E5F0076207D /* PinterCell.swift */; }; + 646640391F0FD2700076207D /* PinterestCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646640331F0F99470076207D /* PinterestCell.swift */; }; + 6466403A1F0FD2990076207D /* UIView+Constraints.swift in Sources */ = {isa = PBXBuildFile; fileRef = 646640371F0FD05A0076207D /* UIView+Constraints.swift */; }; 646A48511F0CEFDB00088AD0 /* PinterestLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EF21F0BB1D200DAA3B7 /* PinterestLayout.swift */; }; B572E5EF1F0C20D100540F7C /* PinterestHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */; }; B5AFCBAC1F0BB16F00CB71B5 /* PinterestLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -48,6 +51,9 @@ /* Begin PBXFileReference section */ 640095F61F0BC37F006AE391 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = ""; }; 644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedCornersView.swift; sourceTree = ""; }; + 646640331F0F99470076207D /* PinterestCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestCell.swift; sourceTree = ""; }; + 646640351F0F9E5F0076207D /* PinterCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterCell.swift; sourceTree = ""; }; + 646640371F0FD05A0076207D /* UIView+Constraints.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Constraints.swift"; sourceTree = ""; }; B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestHeader.swift; sourceTree = ""; }; B5AFCBA71F0BB16F00CB71B5 /* PinterestLayout.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PinterestLayout.framework; sourceTree = BUILT_PRODUCTS_DIR; }; B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PinterestLayout.h; sourceTree = ""; }; @@ -97,6 +103,7 @@ 640095F61F0BC37F006AE391 /* CollectionViewCell.swift */, 644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */, B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */, + 646640351F0F9E5F0076207D /* PinterCell.swift */, ); name = Views; sourceTree = ""; @@ -141,6 +148,8 @@ children = ( B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */, B5CF9EF21F0BB1D200DAA3B7 /* PinterestLayout.swift */, + 646640331F0F99470076207D /* PinterestCell.swift */, + 646640371F0FD05A0076207D /* UIView+Constraints.swift */, B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */, ); path = PinterestLayout; @@ -285,7 +294,10 @@ files = ( 646A48511F0CEFDB00088AD0 /* PinterestLayout.swift in Sources */, B5CF9EE01F0BB19500DAA3B7 /* PinterestVC.swift in Sources */, + 646640361F0F9E5F0076207D /* PinterCell.swift in Sources */, + 6466403A1F0FD2990076207D /* UIView+Constraints.swift in Sources */, B5CF9EDE1F0BB19500DAA3B7 /* AppDelegate.swift in Sources */, + 646640391F0FD2700076207D /* PinterestCell.swift in Sources */, 640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */, 644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */, B572E5EF1F0C20D100540F7C /* PinterestHeader.swift in Sources */, diff --git a/PinterestLayout/PinterestCell.swift b/PinterestLayout/PinterestCell.swift new file mode 100644 index 0000000..105dae7 --- /dev/null +++ b/PinterestLayout/PinterestCell.swift @@ -0,0 +1,188 @@ +// +// PinterestCell.swift +// PinterestLayout +// +// Created by Khrystyna Shevchuk on 7/7/17. +// Copyright © 2017 MagicLab. All rights reserved. +// + +import UIKit + + +public class PinterestCell: UICollectionViewCell { + + private var _roundedCornersView: UIView? + public var roundedCornersView: UIView { + get { + if let roundedCornersView = _roundedCornersView { + return roundedCornersView + } + let roundedCornersView = UIView(frame: bounds) + _roundedCornersView = roundedCornersView + + contentView.addSubview(roundedCornersView) + roundedCornersView.addConstraintsAlignedToSuperview() + + roundedCornersView.clipsToBounds = true + roundedCornersView.layer.cornerRadius = 12 + roundedCornersView.backgroundColor = .white + + return roundedCornersView + } + } + + private var _imageView: UIImageView? + public var imageView: UIImageView { + get { + if let imageView = _imageView { + return imageView + } + let imageView = UIImageView(frame: roundedCornersView.bounds) + _imageView = imageView + + roundedCornersView.addSubview(imageView) + self.addConstraintsForImageView() + + imageView.contentMode = .scaleAspectFit + + return imageView + } + } + + private var _descriptionLabel: UILabel? + public var descriptionLabel: UILabel { + get { + if let descriptionLabel = _descriptionLabel { + return descriptionLabel + } + let descriptionLabel = UILabel() + _descriptionLabel = descriptionLabel + + roundedCornersView.addSubview(descriptionLabel) + + descriptionLabel.numberOfLines = 0 + descriptionLabel.font = UIFont(name: "Arial-ItalicMT", size: 11) + + self.addConstraintsForLabel() + + return descriptionLabel + } + } + + fileprivate var imageViewHeightLayoutConstraint: NSLayoutConstraint? + fileprivate var imageHeight: CGFloat! + + override public func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) { + super.apply(layoutAttributes) + if let attributes = layoutAttributes as? PinterestLayoutAttributes { + imageHeight = attributes.imageHeight + if let imageViewHeightLayoutConstraint = self.imageViewHeightLayoutConstraint { + imageViewHeightLayoutConstraint.constant = attributes.imageHeight + } + } + } +} + + +extension PinterestCell { + + func addConstraintsForImageView() { + imageView.translatesAutoresizingMaskIntoConstraints = false + + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: imageView, + attribute: .top, + relatedBy: .equal, + toItem: roundedCornersView, + attribute: NSLayoutAttribute.top, + multiplier: 1, + constant: 0 + ) + ) + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: imageView, + attribute: .leading, + relatedBy: .equal, + toItem: roundedCornersView, + attribute: .leading, + multiplier: 1, + constant: 0 + ) + ) + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: imageView, + attribute: .trailing, + relatedBy: .equal, + toItem: roundedCornersView, + attribute: .trailing, + multiplier: 1, + constant: 0 + ) + ) + + let imageViewHeightLayoutConstraint = + NSLayoutConstraint( + item: imageView, + attribute: .height, + relatedBy: .equal, + toItem: nil, + attribute: .notAnAttribute, + multiplier: 1, + constant: imageHeight + ) + imageView.addConstraint(imageViewHeightLayoutConstraint) + self.imageViewHeightLayoutConstraint = imageViewHeightLayoutConstraint + } + + func addConstraintsForLabel() { + descriptionLabel.translatesAutoresizingMaskIntoConstraints = false + + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: descriptionLabel, + attribute: .top, + relatedBy: .equal, + toItem: imageView, + attribute: .bottom, + multiplier: 1, + constant: 4 + ) + ) + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: descriptionLabel, + attribute: .left, + relatedBy: .equal, + toItem: roundedCornersView, + attribute: .left, + multiplier: 1, + constant: 4 + ) + ) + roundedCornersView.addConstraint( + NSLayoutConstraint( + item: descriptionLabel, + attribute: .right, + relatedBy: .equal, + toItem: roundedCornersView, + attribute: .right, + multiplier: 1, + constant: 4 + ) + ) + let bottomConstraint = NSLayoutConstraint( + item: roundedCornersView, + attribute: .bottom, + relatedBy: .greaterThanOrEqual, + toItem: descriptionLabel, + attribute: .bottom, + multiplier: 1, + constant: 4 + ) + bottomConstraint.priority = 750 + roundedCornersView.addConstraint(bottomConstraint) + } +} diff --git a/PinterestLayout/UIView+Constraints.swift b/PinterestLayout/UIView+Constraints.swift new file mode 100644 index 0000000..a046fed --- /dev/null +++ b/PinterestLayout/UIView+Constraints.swift @@ -0,0 +1,62 @@ +// +// UIView+Constraints.swift +// PinterestLayout +// +// Created by Khrystyna Shevchuk on 7/7/17. +// Copyright © 2017 MagicLab. All rights reserved. +// + +import UIKit + + +extension UIView { + + func addConstraintsAlignedToSuperview(top: CGFloat = 0, leading: CGFloat = 0, bottom: CGFloat = 0, trailing: CGFloat = 0) { + translatesAutoresizingMaskIntoConstraints = false + + superview?.addConstraint( + NSLayoutConstraint( + item: self, + attribute: .top, + relatedBy: .equal, + toItem: superview, + attribute: .top, + multiplier: 1, + constant: top + ) + ) + superview?.addConstraint( + NSLayoutConstraint( + item: self, + attribute: .leading, + relatedBy: .equal, + toItem: superview, + attribute: .leading, + multiplier: 1, + constant: leading + ) + ) + superview?.addConstraint( + NSLayoutConstraint( + item: self, + attribute: .bottom, + relatedBy: .equal, + toItem: superview, + attribute: .bottom, + multiplier: 1, + constant: bottom + ) + ) + superview?.addConstraint( + NSLayoutConstraint( + item: self, + attribute: .trailing, + relatedBy: .equal, + toItem: superview, + attribute: .trailing, + multiplier: 1, + constant: trailing + ) + ) + } +} diff --git a/PinterestLayoutExample/PinterestVC.swift b/PinterestLayoutExample/PinterestVC.swift index 440d0dc..e9a7510 100644 --- a/PinterestLayoutExample/PinterestVC.swift +++ b/PinterestLayoutExample/PinterestVC.swift @@ -11,6 +11,9 @@ import PinterestLayout import AVFoundation +private let cellIdentifier = "PinterestLayout.PinterestCell" + + class PinterestVC: UICollectionViewController { var images: [UIImage] = [#imageLiteral(resourceName: "new_york"), #imageLiteral(resourceName: "bigben_river"), #imageLiteral(resourceName: "dubai"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "tiger"), #imageLiteral(resourceName: "5"), #imageLiteral(resourceName: "adelaide_castle"), #imageLiteral(resourceName: "rys"), #imageLiteral(resourceName: "12"), #imageLiteral(resourceName: "cat"), #imageLiteral(resourceName: "6"), #imageLiteral(resourceName: "city"), #imageLiteral(resourceName: "9"), #imageLiteral(resourceName: "ph"), #imageLiteral(resourceName: "paris"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "bridge"), #imageLiteral(resourceName: "elephants"), #imageLiteral(resourceName: "venice"), #imageLiteral(resourceName: "bw_new_york"), #imageLiteral(resourceName: "10"), #imageLiteral(resourceName: "kittens"), #imageLiteral(resourceName: "deer"), #imageLiteral(resourceName: "11"), #imageLiteral(resourceName: "8"), #imageLiteral(resourceName: "13"), #imageLiteral(resourceName: "squirell"), #imageLiteral(resourceName: "lion"), #imageLiteral(resourceName: "nature"), #imageLiteral(resourceName: "4")] @@ -21,8 +24,13 @@ class PinterestVC: UICollectionViewController { setupCollectionViewInsets() setupLayout() + + collectionView?.register( + PinterestCell.self, + forCellWithReuseIdentifier: "PinterestCell" + ) } - + //MARK: private private func setupCollectionViewInsets() { @@ -53,16 +61,14 @@ extension PinterestVC { } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCell( + withReuseIdentifier: "PinterestCell", + for: indexPath) as! PinterestCell let image = images[indexPath.item] - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionViewCell - cell.image = image - if indexPath.item % 2 == 0 { - cell.descriptionLabel.text = "Very short description" - } else { - cell.descriptionLabel.text = "Tonight, there are no lovers walking down the park alleys. There are no kings in the castles, and the princesses die alone – they have no frogs or peas. Tonight, the inkpots are empty, and the words are uncountable. Tonight, all shouts are muffled by unbearable silence..." - } + cell.imageView.image = image + cell.descriptionLabel.text = "text" return cell }