-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added PinterestCell to PinterestLayout
- added setting cell programmatically to PinterestLayout
- Loading branch information
1 parent
88dc258
commit 2454236
Showing
4 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
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
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
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) | ||
} | ||
} |
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
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 | ||
) | ||
) | ||
} | ||
} |
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