Skip to content
A UICollectionView grid layout designed to support Dynamic Type by allowing the height of each row to size to fit content.
Swift Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Release 1.2.2 Sep 17, 2019
FlexibleRowHeightGridLayout Release 1.2.2 Sep 17, 2019
docs/images Updated layout example images Aug 20, 2019
.gitignore Updated .gitignore Aug 7, 2019
.travis.yml Release 0.1.0 Aug 6, 2019
CHANGELOG.md Release 1.2.2 Sep 17, 2019
FlexibleRowHeightGridLayout.podspec Release 1.2.2 Sep 17, 2019
LICENSE Release 0.1.0 Aug 6, 2019
Package.swift Release 0.2.0 Aug 7, 2019
README.md Adding syntax highlighting Aug 20, 2019
_Pods.xcodeproj Release 0.1.0 Aug 6, 2019
install_swiftlint.sh Release 0.1.0 Aug 6, 2019

README.md

FlexibleRowHeightGridLayout

CI Status Version Carthage compatible License Platform Twitter Swift 5.0

FlexibleRowHeightGridLayout is a UICollectionViewLayout which lays out self-sizing cells in a grid and is designed to support accessibility, in particular, Dynamic Type. It is designed to automatically re-layout with changes in text size on the device (UIContentSizeCategory). Row heights are flexible with this layout i.e. each row may have different height where the height of the row is determined by the tallest cell in the row so that the row height will always fit the content within the row.

Illustration of flexible row heights

To learn more about how to use FlexibleRowHeightGridLayout take a look at the blog post or make use of the table of contents below:

Features

  • Grid layout supporting changes in text size - will automatically re-layout with changes in UIContentSizeCategory (Dynamic Type).
  • Supports self-sizing UICollectionViewCells.
  • Supports sections including headers and / or footers.

Requirements

FlexibleRowHeightGridLayout is written in Swift 5.0 and is available on iOS 8.0 or higher.

Installation

Cocoapods

CocoaPods is a dependency manager which integrates dependencies into your Xcode workspace. To install it using Ruby gems run:

gem install cocoapods

To install FlexibleRowHeightGridLayout using Cocoapods, simply add the following line to your Podfile:

pod "FlexibleRowHeightGridLayout"

Then run the command:

pod install

For more information see here.

Carthage

Carthage is a dependency manager which produces a binary for manual integration into your project. It can be installed via Homebrew using the commands:

brew update
brew install carthage

In order to integrate FlexibleRowHeightGridLayout into your project via Carthage, add the following line to your project's Cartfile:

github "rwbutler/FlexibleRowHeightGridLayout"

From the macOS Terminal run carthage update --platform iOS to build the framework then drag FlexibleRowHeightGridLayout.framework into your Xcode project.

For more information see here.

Swift Package Manager

Xcode 11 includes support for Swift Package Manager. In order to add FlexibleRowHeightGridLayout to your project in Xcode 11, from the File menu select Swift Packages and then select Add Package Dependency.

A dialogue will request the package repository URL which is:

https://github.com/rwbutler/FlexibleRowHeightGridLayout

After verifying the URL, Xcode will prompt you to select whether to pull a specific branch, commit or versioned release into your project.

Proceed to the next step by where you will be asked to select the package product to integrate into a target. There will be a single package product named FlexibleRowHeightGridLayout which should be pre-selected. Ensure that your main app target is selected from the rightmost column of the dialog then click Finish to complete the integration.

Usage

In order to have your UICollectionView make use of FlexibleRowHeightGridLayout to layout content you may either instantiate a new instance and assign it to the collection view's collectionViewLayout property if your collection view is defined in Interface Builder as follows:

let layout = FlexibleRowHeightGridLayout()
layout.delegate = self
collectionView.collectionViewLayout = layout
// Following line is only required if content has already been loaded before collectionViewLayout property set.
collectionView.reloadData()

Or, if your UICollectionView is instantiated programmatically then you may pass the layout to the UICollectionView's initializer:

let layout = FlexibleRowHeightGridLayout()
layout.delegate = self
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

Notice that in both cases you are required to provide an implementation of the layout's delegate -FlexibleRowHeightGridLayoutDelegate.

FlexibleRowHeightGridLayoutDelegate

The delegate defines two methods which must be implemented in order to allow FlexibleRowHeightGridLayout to layout items correctly. These are:

  • func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, heightForItemAt indexPath: IndexPath) -> CGFloat

Should return the height of the item for the given IndexPath. Using this information the layout is able to calculate the correct height for the row. When calculating the height of text, you find it useful to make use of NSString's func boundingRect(with size: options: attributes: context:) -> CGRect function as follows:

let constraintRect = CGSize(width: columnWidth, height: .greatestFiniteMagnitude)
let textHeight = "Some text".boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).height

In order to help you determine the height of your content, FlexibleRowHeightGridLayout provides a couple of useful methods such as textHeight(_ text: String, font: UIFont) to help you calculate the height required to display a String rendered using the specified font. If your cell were to contain a label pinned to each of the edges of the cell’s content view then the height of the cell’s content could easily be calculated as follows:

func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, heightForItemAt indexPath: IndexPath) -> CGFloat {
    let text = dataSource.item(at: indexPath.item)
    let font = UIFont.preferredFont(forTextStyle: .body)
    return layout.textHeight(text, font: font)
}

These helper methods are particular useful for developers who are already using TypographyKit (a framework which also supports Dynamic Type by automatically updating the text size on UIKit elements when the users changes the text size preference on their device). Calculating the height of a cell containing a single label can be achieved as follows:

func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, heightForItemAt indexPath: IndexPath) -> CGFloat {
    let text = dataSource.item(at: indexPath.item)
    let font = Typography(for: .cellText).font()
    return layout.textHeight(text, font: font)
}

Or if your cell is defined in a nib, then it is possible to inflate a cell in order to calculate the cell height as follows:

let text = dataSource.item(at: indexPath.item)
guard let nib = Bundle.main.loadNibNamed("CustomCell", owner: CustomCell.self, options: nil), let cell = nib?[0] as? CustomCell else {
    return
}

// Ensure that your content has been set
cell.label.text = text

// Assuming your custom cell has a content view
cell.contentView.setNeedsLayout()
cell.contentView.layoutIfNeeded()
let size = cell.contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
return size.height
  • func numberOfColumns(for size: CGSize) -> Int

Should return the number of columns in the UICollectionView's grid when the UICollectionView is of the specified size. For example, the UICollectionView may have larger bounds when the device is in landscape and therefore you may want your UICollectionView to have 4 columns when the device is in landscape orientation and only 3 when in portrait.

There are another two delegate methods which may optionally be implemented should you wish to include a header and / or footer as part of your UICollectionView:

  • @objc optional func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, referenceHeightForHeaderInSection section: Int) -> CGFloat

Should return the height of the header in your UICollectionView. If the value returned from this function is zero than no header will be added.

  • @objc optional func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, referenceHeightForFooterInSection section: Int) -> CGFloat

Should return the height of the footer in your UICollectionView. If the value returned from this function is zero than no footer will be added.

FAQS

Does this layout support section headers and / or footers?

Yes, in order to add a section header and / or footer to your UICollectionView ensure that you provide an implementation for the two optional delegate methods in FlexibleRowHeightGridLayoutDelegate:

  • @objc optional func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, referenceHeightForHeaderInSection section: Int) -> CGFloat

  • @objc optional func collectionView(_ collectionView: UICollectionView, layout: FlexibleRowHeightGridLayout, referenceHeightForFooterInSection section: Int) -> CGFloat

Author

Ross Butler

License

FlexibleRowHeightGridLayout is available under the MIT license. See the LICENSE file for more info.

Additional Software

Controls

AnimatedGradientView
AnimatedGradientView

Frameworks

  • Cheats - Retro cheat codes for modern iOS apps.
  • Connectivity - Improves on Reachability for determining Internet connectivity in your iOS application.
  • FeatureFlags - Allows developers to configure feature flags, run multiple A/B or MVT tests using a bundled / remotely-hosted JSON configuration file.
  • FlexibleRowHeightGridLayout - A UICollectionView grid layout designed to support Dynamic Type by allowing the height of each row to size to fit content.
  • Skylark - Fully Swift BDD testing framework for writing Cucumber scenarios using Gherkin syntax.
  • TailorSwift - A collection of useful Swift Core Library / Foundation framework extensions.
  • TypographyKit - Consistent & accessible visual styling on iOS with Dynamic Type support.
  • Updates - Automatically detects app updates and gently prompts users to update.
Cheats Connectivity FeatureFlags Skylark TypographyKit Updates
Cheats Connectivity FeatureFlags Skylark TypographyKit Updates

Tools

  • Config Validator - Config Validator validates & uploads your configuration files and cache clears your CDN as part of your CI process.
  • IPA Uploader - Uploads your apps to TestFlight & App Store.
  • Palette - Makes your TypographyKit color palette available in Xcode Interface Builder.
Config Validator IPA Uploader Palette
Config Validator IPA Uploader Palette
You can’t perform that action at this time.