All Projects → MagicLab-team → Pinterestlayout

MagicLab-team / Pinterestlayout

Licence: mit
Custom collection view layout inspired by Pinterest layout. Written in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Pinterestlayout

Longshadow
Add a long shadow on any Android View
Stars: ✭ 562 (+156.62%)
Mutual labels:  layout, custom
Shapeofview
Give a custom shape to any android view, Material Design 2 ready
Stars: ✭ 2,977 (+1259.36%)
Mutual labels:  layout, custom
bubble-layout
An Android ViewGroup that displays avatar bubbles... similar to the chat bubbles on Facebook Messenger.
Stars: ✭ 46 (-79%)
Mutual labels:  custom, layout
Fapaginationlayout
Collection view pagination layout
Stars: ✭ 276 (+26.03%)
Mutual labels:  layout, collectionview
Waterfall.js
Tired of use creepy hacks or heavy ways to get a Grid based on Pinterest?
Stars: ✭ 458 (+109.13%)
Mutual labels:  pinterest, layout
Bouncylayout
Make. It. Bounce.
Stars: ✭ 4,035 (+1742.47%)
Mutual labels:  layout, collectionview
Containercontroller
UI Component. This is a copy swipe-panel from app: Apple Maps, Stocks. Swift version
Stars: ✭ 273 (+24.66%)
Mutual labels:  layout, collectionview
Collection View Layouts
A library that implements custom flow layouts for iOS apps
Stars: ✭ 491 (+124.2%)
Mutual labels:  pinterest, collectionview
Arclayout
With Arc Layout explore new styles and approaches on material design
Stars: ✭ 1,662 (+658.9%)
Mutual labels:  layout, custom
Sharex Upload Server
AKA ShareS - Feature full & Stable ShareX and file server in node. Includes images, videos, code, text, markdown rendering, password protected uploads, logging via discord, administration through Discord, url shortening, and a full front end. Use standalone or via reverse proxy
Stars: ✭ 180 (-17.81%)
Mutual labels:  custom
Arranged
Open source replacement of UIStackView for iOS 8 (100% layouts supported)
Stars: ✭ 202 (-7.76%)
Mutual labels:  layout
Postwill
Posting to the most popular social media from Ruby
Stars: ✭ 181 (-17.35%)
Mutual labels:  pinterest
Chatlayout
ChatLayout is an alternative solution to MessageKit. It uses custom UICollectionViewLayout to provide you full control over the presentation as well as all the tools available in UICollectionView. It supports dynamic cells and supplementary view sizes.
Stars: ✭ 184 (-15.98%)
Mutual labels:  collectionview
Layouts
Grab-and-go layouts for React
Stars: ✭ 202 (-7.76%)
Mutual labels:  layout
Val
VirtualDOM abstraction layer - give yourself better integration and full control over the DOM with any virtual DOM library that uses a Hyperscript-like API such as React and Preact.
Stars: ✭ 181 (-17.35%)
Mutual labels:  custom
Customasm
💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Stars: ✭ 211 (-3.65%)
Mutual labels:  custom
Grid
This package has moved and renamed
Stars: ✭ 2,079 (+849.32%)
Mutual labels:  layout
Shuffle
Categorize, sort, and filter a responsive grid of items
Stars: ✭ 2,170 (+890.87%)
Mutual labels:  layout
Kingcercode
单个输入框验证码样式
Stars: ✭ 216 (-1.37%)
Mutual labels:  custom
Forwardemail.net
The best free email forwarding for custom domains (Web Server)
Stars: ✭ 211 (-3.65%)
Mutual labels:  custom

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:

$ 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:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'PinterestLayout'
end

Then, run the following command:

$ 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.
import UIKit
import PinterestLayout

class MyPinterestVC: PinterestVC {
    
    override public func viewDidLoad() {
        super.viewDidLoad()
        
        let text = "Some text. Some text. Some text. Some text."
        
        items = [
            PinterestItem(image: UIImage(named: "new_york"), text: text),
            PinterestItem(image: UIImage(named: "bigben_river"), text: text),
            PinterestItem(image: UIImage(named: "dubai"), text: text),
            PinterestItem(image: UIImage(named: "4"), text: text),
            PinterestItem(image: UIImage(named: "tiger"), text: text)
        ]
    }
}

Using custom cell

0 - Import PinterestLayout

import PinterestLayout

1 - Set PinterestLayout to your collection view.

let layout = PinterestLayout()
collectionView.collectionViewLayout = layout

2 - Setup layout

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

3 - Implement methods of PinterestLayoutDelegate

/**
Height for image view in cell.
     
@param collectionView - collectionView
@param indexPath - index path for cell
     
Returns height of image view.
*/
func collectionView(collectionView: UICollectionView,
                    heightForImageAtIndexPath indexPath: IndexPath,
                    withWidth: CGFloat) -> CGFloat
    
/**
Height for annotation view (label) in cell.
     
@param collectionView - collectionView
@param indexPath - index path for cell
     
Returns height of annotation view.
*/
func collectionView(collectionView: UICollectionView,
                    heightForAnnotationAtIndexPath indexPath: IndexPath,
                    withWidth: CGFloat) -> CGFloat
  • PinterestLayout provides public API to easily calculate the best imageview and text heigths based on available width.
public extension UIImage {
    /**
     Calculates the best height of the image for available width.
     */
    public func height(forWidth width: CGFloat) -> CGFloat
//...
public extension String {
    /**
     Calculates the best height of the text for available width and font used.
     */
    public func heightForWidth(width: CGFloat, font: UIFont) -> CGFloat 
  • So implementation of PinterestLayoutDelegate might be:
extension CustomCollectionVC: PinterestLayoutDelegate {
    
    func collectionView(collectionView: UICollectionView,
                        heightForImageAtIndexPath indexPath: IndexPath,
                        withWidth: CGFloat) -> CGFloat {
        let image = images[indexPath.item]
        
        return image.height(forWidth: withWidth)
    }
    
    func collectionView(collectionView: UICollectionView,
                        heightForAnnotationAtIndexPath indexPath: IndexPath,
                        withWidth: CGFloat) -> CGFloat {
        let textFont = UIFont(name: "Arial-ItalicMT", size: 11)!                
        return "Some text".heightForWidth(width: withWidth, font: textFont)
    }
}

4 - Create custom cell and apply PinterestLayoutAttributes

class CollectionViewCell: UICollectionViewCell {
//...
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
        super.apply(layoutAttributes)
        if let attributes = layoutAttributes as? PinterestLayoutAttributes {
            //change image view height by changing its constraint
            imageViewHeightLayoutConstraint.constant = attributes.imageHeight
        }
    }
}

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)
{
    "src": "/upload/resize_cache/iblock/8e7/204_265_2/8e7f1f04d5e835f596ef33da74946847.jpg",
    "width": 204,
    "height": 265
}
  1. when data is loaded invalidate layout as well as reload data on collection view.
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadData()

Contact us

Contact our team on email - [email protected]

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].