项目作者: MagicLab-team

项目描述 :
Custom collection view layout inspired by Pinterest layout. Written in Swift.
高级语言: Swift
项目地址: git://github.com/MagicLab-team/PinterestLayout.git
创建时间: 2017-07-04T11:19:31Z
项目社区:https://github.com/MagicLab-team/PinterestLayout

开源协议:MIT License

下载


PinterestLayout

Custom collection view layout with different image and text sizes.

PinterestVC Custom Cell
Demo Demo

Contents

Requirements

  • iOS 8.0+
  • Swift 3.0+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

  1. $ gem install cocoapods

CocoaPods 1.1.0+ is required to build Reusable 1.0.0+.

To integrate Reusable into your Xcode project using CocoaPods, specify it in your Podfile:

  1. source 'https://github.com/CocoaPods/Specs.git'
  2. platform :ios, '8.0'
  3. use_frameworks!
  4. target '<Your Target Name>' do
  5. pod 'PinterestLayout'
  6. end

Then, run the following command:

  1. $ pod install

Usage

PinterestVC subclass

  • All you need to do is to sublcass from PinterestVC and provide items to be shown.
  • PinterestVC will calculate image and text sizes.
  1. import UIKit
  2. import PinterestLayout
  3. class MyPinterestVC: PinterestVC {
  4. override public func viewDidLoad() {
  5. super.viewDidLoad()
  6. let text = "Some text. Some text. Some text. Some text."
  7. items = [
  8. PinterestItem(image: UIImage(named: "new_york"), text: text),
  9. PinterestItem(image: UIImage(named: "bigben_river"), text: text),
  10. PinterestItem(image: UIImage(named: "dubai"), text: text),
  11. PinterestItem(image: UIImage(named: "4"), text: text),
  12. PinterestItem(image: UIImage(named: "tiger"), text: text)
  13. ]
  14. }
  15. }

Using custom cell

0 - Import PinterestLayout

  1. import PinterestLayout

1 - Set PinterestLayout to your collection view.

  1. let layout = PinterestLayout()
  2. collectionView.collectionViewLayout = layout

2 - Setup layout

  1. layout.delegate = self
  2. layout.cellPadding = 5
  3. layout.numberOfColumns = 2

3 - Implement methods of PinterestLayoutDelegate

  1. /**
  2. Height for image view in cell.
  3. @param collectionView - collectionView
  4. @param indexPath - index path for cell
  5. Returns height of image view.
  6. */
  7. func collectionView(collectionView: UICollectionView,
  8. heightForImageAtIndexPath indexPath: IndexPath,
  9. withWidth: CGFloat) -> CGFloat
  10. /**
  11. Height for annotation view (label) in cell.
  12. @param collectionView - collectionView
  13. @param indexPath - index path for cell
  14. Returns height of annotation view.
  15. */
  16. func collectionView(collectionView: UICollectionView,
  17. heightForAnnotationAtIndexPath indexPath: IndexPath,
  18. withWidth: CGFloat) -> CGFloat
  • PinterestLayout provides public API to easily calculate the best imageview and text heigths based on available width.
  1. public extension UIImage {
  2. /**
  3. Calculates the best height of the image for available width.
  4. */
  5. public func height(forWidth width: CGFloat) -> CGFloat
  6. //...
  7. public extension String {
  8. /**
  9. Calculates the best height of the text for available width and font used.
  10. */
  11. public func heightForWidth(width: CGFloat, font: UIFont) -> CGFloat
  • So implementation of PinterestLayoutDelegate might be:

    1. extension CustomCollectionVC: PinterestLayoutDelegate {
    2. func collectionView(collectionView: UICollectionView,
    3. heightForImageAtIndexPath indexPath: IndexPath,
    4. withWidth: CGFloat) -> CGFloat {
    5. let image = images[indexPath.item]
    6. return image.height(forWidth: withWidth)
    7. }
    8. func collectionView(collectionView: UICollectionView,
    9. heightForAnnotationAtIndexPath indexPath: IndexPath,
    10. withWidth: CGFloat) -> CGFloat {
    11. let textFont = UIFont(name: "Arial-ItalicMT", size: 11)!
    12. return "Some text".heightForWidth(width: withWidth, font: textFont)
    13. }
    14. }

4 - Create custom cell and apply PinterestLayoutAttributes

  • Example is here
    1. class CollectionViewCell: UICollectionViewCell {
    2. //...
    3. override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    4. super.apply(layoutAttributes)
    5. if let attributes = layoutAttributes as? PinterestLayoutAttributes {
    6. //change image view height by changing its constraint
    7. imageViewHeightLayoutConstraint.constant = attributes.imageHeight
    8. }
    9. }
    10. }

Tips

Headers/Footers

PinterestLayout fully supports collection view headers and footers. So you can have many sections.

When you load data from service

In case you load data asynchronously please follow next steps:

  1. Do not use PinterestVC as parent class to your view controller. Use custom cell approach.
  2. Make sure server returns image sorce with its sizes (height and width)
    1. {
    2. "src": "/upload/resize_cache/iblock/8e7/204_265_2/8e7f1f04d5e835f596ef33da74946847.jpg",
    3. "width": 204,
    4. "height": 265
    5. }
  3. when data is loaded invalidate layout as well as reload data on collection view.
    1. collectionView.collectionViewLayout.invalidateLayout()
    2. collectionView.reloadData()

Contact us

Contact our team on email - roman.sorochak@gmail.com

License

PinterestLayout is released under the MIT license. See LICENSE for details.