Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Source/TagCellLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class TagCellLayout: UICollectionViewLayout {
var layoutInfoList: [LayoutInfo] = []
var numberOfTagsInCurrentRow = 0
var currentTagIndex: Int = 0


weak var delegate: TagCellLayoutDelegate?

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Source/TagCellLayoutDelegate.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions TagCellLayout/TagCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions TagCellLayout/TagCollectionViewCell.xib
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -20,13 +20,14 @@
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Tags" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="XTE-Bs-m9s">
<rect key="frame" x="0.0" y="0.0" width="142" height="58"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</view>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="XTE-Bs-m9s" secondAttribute="trailing" id="MeS-8d-ODJ"/>
<constraint firstAttribute="bottom" secondItem="XTE-Bs-m9s" secondAttribute="bottom" id="Tag-Ll-ceU"/>
Expand Down
14 changes: 12 additions & 2 deletions TagCellLayout/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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!)
Expand Down