From eb49edbf8c725c1316efefd72bd7f9eb64994ba3 Mon Sep 17 00:00:00 2001 From: Chris Rusin Date: Sat, 26 Jan 2019 09:35:15 +0100 Subject: [PATCH 1/2] Update example project to make it more clear --- TagCellLayout/TagCollectionViewCell.swift | 7 +++++++ TagCellLayout/TagCollectionViewCell.xib | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/TagCellLayout/TagCollectionViewCell.swift b/TagCellLayout/TagCollectionViewCell.swift index db82b43..f21e909 100755 --- a/TagCellLayout/TagCollectionViewCell.swift +++ b/TagCellLayout/TagCollectionViewCell.swift @@ -18,8 +18,15 @@ class TagCollectionViewCell: UICollectionViewCell { tagView.layer.cornerRadius = tagView.frame.size.height/2 - 2 tagView.layer.borderColor = UIColor.blue.cgColor tagView.layer.borderWidth = 3.0 + tagView.layer.masksToBounds = true } } + + override var isSelected: Bool { + didSet { + tagView?.backgroundColor = isSelected ? .blue : .clear + } + } func configure(with text: String) { tagView?.text = text diff --git a/TagCellLayout/TagCollectionViewCell.xib b/TagCellLayout/TagCollectionViewCell.xib index b5c8732..acaf373 100755 --- a/TagCellLayout/TagCollectionViewCell.xib +++ b/TagCellLayout/TagCollectionViewCell.xib @@ -1,11 +1,11 @@ - + - + @@ -20,13 +20,14 @@ + From c4604de34d1fa849ec5d3cf505bed4e8dcd3ce9c Mon Sep 17 00:00:00 2001 From: Chris Rusin Date: Sat, 26 Jan 2019 09:35:54 +0100 Subject: [PATCH 2/2] Add interitem spacing --- Source/TagCellLayout.swift | 6 +++--- Source/TagCellLayoutDelegate.swift | 2 ++ TagCellLayout/ViewController.swift | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) mode change 100644 => 100755 Source/TagCellLayoutDelegate.swift diff --git a/Source/TagCellLayout.swift b/Source/TagCellLayout.swift index f6a1cfe..82f6f61 100755 --- a/Source/TagCellLayout.swift +++ b/Source/TagCellLayout.swift @@ -16,7 +16,6 @@ public class TagCellLayout: UICollectionViewLayout { var layoutInfoList: [LayoutInfo] = [] var numberOfTagsInCurrentRow = 0 var currentTagIndex: Int = 0 - weak var delegate: TagCellLayoutDelegate? @@ -72,7 +71,7 @@ private extension TagCellLayout { var currentTagFrame: CGRect { guard let info = currentTagLayoutInfo?.layoutAttribute else { return .zero } var frame = info.frame - frame.origin.x += info.bounds.width + frame.origin.x += info.bounds.width + (delegate?.tagCellLayoutInteritemHorizontalSpacing(layout: self) ?? 0.0) return frame } @@ -146,7 +145,8 @@ private extension TagCellLayout { // if next tag goes out of screen then move it to next row if shouldMoveTagToNextRow(tagWidth: tagSize.width) { tagFrame.origin.x = 0.0 - tagFrame.origin.y += currentTagFrame.height + tagFrame.origin.y += currentTagFrame.height + + (delegate?.tagCellLayoutInteritemVerticalSpacing(layout: self) ?? 0.0) isFirstElementInARow = true } let attribute = layoutAttribute(tagIndex: tagIndex, tagFrame: tagFrame) diff --git a/Source/TagCellLayoutDelegate.swift b/Source/TagCellLayoutDelegate.swift old mode 100644 new mode 100755 index 98c50d6..8548feb --- a/Source/TagCellLayoutDelegate.swift +++ b/Source/TagCellLayoutDelegate.swift @@ -12,4 +12,6 @@ import UIKit public protocol TagCellLayoutDelegate: NSObjectProtocol { func tagCellLayoutTagSize(layout: TagCellLayout, atIndex index:Int) -> CGSize + func tagCellLayoutInteritemHorizontalSpacing(layout: TagCellLayout) -> CGFloat + func tagCellLayoutInteritemVerticalSpacing(layout: TagCellLayout) -> CGFloat } diff --git a/TagCellLayout/ViewController.swift b/TagCellLayout/ViewController.swift index 08ef96e..11d3bb2 100755 --- a/TagCellLayout/ViewController.swift +++ b/TagCellLayout/ViewController.swift @@ -24,6 +24,8 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + collectionView?.allowsSelection = true + collectionView?.allowsMultipleSelection = true // Do any additional setup after loading the view, typically from a nib. } @@ -33,7 +35,8 @@ class ViewController: UIViewController { // THIS IS ALL WHAT IS REQUIRED TO SETUP YOUR TAGS - let tagCellLayout = TagCellLayout(alignment: .center, delegate: self) + let tagCellLayout = TagCellLayout(alignment: .left, delegate: self) + tagCellLayout.delegate = self collectionView?.collectionViewLayout = tagCellLayout } @@ -70,7 +73,14 @@ extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource { } extension ViewController: TagCellLayoutDelegate { - + func tagCellLayoutInteritemHorizontalSpacing(layout: TagCellLayout) -> CGFloat { + return 10.0 + } + + func tagCellLayoutInteritemVerticalSpacing(layout: TagCellLayout) -> CGFloat { + return 10.0 + } + func tagCellLayoutTagSize(layout: TagCellLayout, atIndex index: Int) -> CGSize { if index == longTagIndex || index == (longTagIndex + 3) { var s = textSize(text: longString, font: UIFont.systemFont(ofSize: 17.0), collectionView: collectionView!)