Skip to content

Commit

Permalink
- added PinterestCell to PinterestLayout
Browse files Browse the repository at this point in the history
- added setting cell programmatically to PinterestLayout
  • Loading branch information
KhrystynaShevchuk committed Jul 7, 2017
1 parent 88dc258 commit 2454236
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 8 deletions.
12 changes: 12 additions & 0 deletions PinterestLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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, ); }; };
Expand Down Expand Up @@ -48,6 +51,9 @@
/* Begin PBXFileReference section */
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedCornersView.swift; sourceTree = "<group>"; };
646640331F0F99470076207D /* PinterestCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestCell.swift; sourceTree = "<group>"; };
646640351F0F9E5F0076207D /* PinterCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterCell.swift; sourceTree = "<group>"; };
646640371F0FD05A0076207D /* UIView+Constraints.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Constraints.swift"; sourceTree = "<group>"; };
B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestHeader.swift; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -97,6 +103,7 @@
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */,
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */,
B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */,
646640351F0F9E5F0076207D /* PinterCell.swift */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -141,6 +148,8 @@
children = (
B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */,
B5CF9EF21F0BB1D200DAA3B7 /* PinterestLayout.swift */,
646640331F0F99470076207D /* PinterestCell.swift */,
646640371F0FD05A0076207D /* UIView+Constraints.swift */,
B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */,
);
path = PinterestLayout;
Expand Down Expand Up @@ -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 */,
Expand Down
188 changes: 188 additions & 0 deletions PinterestLayout/PinterestCell.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
62 changes: 62 additions & 0 deletions PinterestLayout/UIView+Constraints.swift
Original file line number Diff line number Diff line change
@@ -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
)
)
}
}
22 changes: 14 additions & 8 deletions PinterestLayoutExample/PinterestVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -21,8 +24,13 @@ class PinterestVC: UICollectionViewController {

setupCollectionViewInsets()
setupLayout()

collectionView?.register(
PinterestCell.self,
forCellWithReuseIdentifier: "PinterestCell"
)
}

//MARK: private

private func setupCollectionViewInsets() {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2454236

Please sign in to comment.