Skip to content

Commit

Permalink
Swift 4 Support
Browse files Browse the repository at this point in the history
Converted to Swift 4
  • Loading branch information
Mohamed Hamed authored and Ritesh Gupta committed Dec 3, 2017
1 parent 8259952 commit 4ad1b0d
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 74 deletions.
Empty file modified LICENSE 100644 → 100755
Empty file.
Empty file modified README.md 100644 → 100755
Empty file.
Empty file modified Source/TagCellLayout+Helper.swift 100644 → 100755
Empty file.
109 changes: 55 additions & 54 deletions Source/TagCellLayout.swift 100644 → 100755
Expand Up @@ -56,7 +56,7 @@ public class TagCellLayout: UICollectionViewLayout {
position.x += info.bounds.width
return position
}
return CGPointZero
return .zero
}

var currentTagLayoutInfo: TagCellLayoutInfo? {
Expand All @@ -68,7 +68,7 @@ public class TagCellLayout: UICollectionViewLayout {
}

var tagsCount: Int {
return collectionView?.numberOfItemsInSection(0) ?? 0
return collectionView?.numberOfItems(inSection: 0) ?? 0
}

var collectionViewWidth: CGFloat {
Expand Down Expand Up @@ -97,39 +97,38 @@ public class TagCellLayout: UICollectionViewLayout {

//MARK: - Override Methods

override public func prepareLayout() {
override public func prepare() {
resetLayoutState()
setupTagCellLayout()
}

override public func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
if layoutInfoList.count > indexPath.row {
return layoutInfoList[indexPath.row].layoutAttribute
}

return nil
}

override public func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
if layoutInfoList.count > indexPath.row {
return layoutInfoList[indexPath.row].layoutAttribute
}

return nil
}

override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
if layoutInfoList.count > 0 {
let visibleLayoutAttributes = layoutInfoList
.map { $0.layoutAttribute }
.filter { CGRectIntersectsRect(rect, $0.frame) }
.filter { rect.intersects($0.frame) }
return visibleLayoutAttributes
}
return nil
}

override public func collectionViewContentSize() -> CGSize {
if let
heightPerLine = delegate?.tagCellLayoutTagFixHeight(self),
width = collectionView?.frame.size.width
{
let height = heightPerLine*CGFloat(lineNumber)
return CGSize(width: width, height: height)
}
return CGSize.zero
}
override public var collectionViewContentSize: CGSize {
if let
heightPerLine = delegate?.tagCellLayoutTagFixHeight(layout: self),
let width = collectionView?.frame.size.width
{
let height = heightPerLine*CGFloat(lineNumber)
return CGSize(width: width, height: height)
}
return CGSize.zero
}

public class func textWidth(text: String, font: UIFont) -> CGFloat {
let padding: CGFloat = 4 // this number is independent of font and is required to compensate the inaccuracy of sizeToFit calculation!
Expand All @@ -148,9 +147,9 @@ private extension TagCellLayout {

func setupTagCellLayout() {
// delegate and collectionview shouldn't be nil
if let delegate = delegate where collectionView != nil {
if let delegate = delegate , let _ = collectionView {
// basic layout setup which is independent of TagAlignment type
basicLayoutSetup(delegate)
basicLayoutSetup(delegate: delegate)

// handle if TagAlignment is other than Left
handleTagAlignment()
Expand Down Expand Up @@ -185,11 +184,11 @@ private extension TagCellLayout {
func createLayoutAttributes() {
if let delegate = delegate {
// calculating tag-size
let tagHeight = delegate.tagCellLayoutTagFixHeight(self)
let tagWidth = delegate.tagCellLayoutTagWidth(self, atIndex: currentTagIndex)
let tagSize = CGSizeMake(tagWidth, tagHeight)
let tagHeight = delegate.tagCellLayoutTagFixHeight(layout: self)
let tagWidth = delegate.tagCellLayoutTagWidth(layout: self, atIndex: currentTagIndex)
let tagSize = CGSize(width: tagWidth, height: tagHeight)

let layoutInfo = tagCellLayoutInfo(currentTagIndex, tagSize: tagSize)
let layoutInfo = tagCellLayoutInfo(tagIndex: currentTagIndex, tagSize: tagSize)
layoutInfoList.append(layoutInfo)
}
}
Expand All @@ -199,11 +198,11 @@ private extension TagCellLayout {
var tagFrame = CGRect(origin: currentTagPosition, size: tagSize)

// if next tag goes out of screen then move it to next row
if shouldMoveTagToNextRow(tagSize.width) {
if shouldMoveTagToNextRow(tagWidth: tagSize.width) {
tagFrame.origin.x = 0
tagFrame.origin.y += tagSize.height
}
let attribute = layoutAttribute(tagIndex, tagFrame: tagFrame)
let attribute = layoutAttribute(tagIndex: tagIndex, tagFrame: tagFrame)
let info = TagCellLayoutInfo(layoutAttribute: attribute)
return info
}
Expand All @@ -213,32 +212,32 @@ private extension TagCellLayout {
}

func layoutAttribute(tagIndex: Int, tagFrame: CGRect) -> UICollectionViewLayoutAttributes {
let indexPath = NSIndexPath(forItem: tagIndex, inSection: 0)
let layoutAttribute = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
let indexPath = IndexPath(item: tagIndex, section: 0)
let layoutAttribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
layoutAttribute.frame = tagFrame
return layoutAttribute
}

func configureWhiteSpace() {
let layoutInfo = layoutInfoList[currentTagIndex].layoutAttribute
let tagWidth = layoutInfo.frame.size.width
if shouldMoveTagToNextRow(tagWidth) {
if shouldMoveTagToNextRow(tagWidth: tagWidth) {
lineNumber += 1
applyWhiteSpace(startingIndex: (currentTagIndex - 1))
}
}

func applyWhiteSpace(startingIndex startingIndex: Int) {
func applyWhiteSpace(startingIndex: Int) {
let lastIndex = startingIndex - numberOfTagsInCurrentRow
let whiteSpace = calculateWhiteSpace(startingIndex)
let whiteSpace = calculateWhiteSpace(tagIndex: startingIndex)

for tagIndex in (lastIndex+1) ..< (startingIndex+1) {
insertWhiteSpace(tagIndex, whiteSpace: whiteSpace)
insertWhiteSpace(tagIndex: tagIndex, whiteSpace: whiteSpace)
}
}

func calculateWhiteSpace(tagIndex: Int) -> CGFloat {
let tagFrame = tagFrameForIndex(tagIndex)
let tagFrame = tagFrameForIndex(tagIndex: tagIndex)
let whiteSpace = collectionViewWidth - (tagFrame.origin.x + tagFrame.size.width)
return whiteSpace
}
Expand All @@ -251,28 +250,30 @@ private extension TagCellLayout {
}

func tagFrameForIndex(tagIndex: Int) -> CGRect {
let tagFrame = tagIndex > -1 ? layoutInfoList[tagIndex].layoutAttribute.frame : CGRectZero
let tagFrame = tagIndex > -1 ? layoutInfoList[tagIndex].layoutAttribute.frame : .zero
return tagFrame
}

func configurePositionForNextTag() {
let layoutInfo = layoutInfoList[currentTagIndex].layoutAttribute
let moveTag = shouldMoveTagToNextRow(layoutInfo.frame.size.width)
let moveTag = shouldMoveTagToNextRow(tagWidth: layoutInfo.frame.size.width)
numberOfTagsInCurrentRow = moveTag ? 1 : (numberOfTagsInCurrentRow + 1)
}

func handleTagAlignment() {
if let collectionView = collectionView where tagAlignmentType != .Left {
let tagsCount = collectionView.numberOfItemsInSection(0)
for tagIndex in 0 ..< tagsCount {
var tagFrame = layoutInfoList[tagIndex].layoutAttribute.frame
let whiteSpace = layoutInfoList[tagIndex].whiteSpace
tagFrame.origin.x += whiteSpace
let tagAttribute = layoutAttribute(tagIndex, tagFrame: tagFrame)
layoutInfoList[tagIndex].layoutAttribute = tagAttribute
}
}
}

func handleTagAlignment() {
if let collectionView = collectionView {
if tagAlignmentType != .Left {
let tagsCount = collectionView.numberOfItems(inSection: 0)
for tagIndex in 0 ..< tagsCount {
var tagFrame = layoutInfoList[tagIndex].layoutAttribute.frame
let whiteSpace = layoutInfoList[tagIndex].whiteSpace
tagFrame.origin.x += whiteSpace
let tagAttribute = layoutAttribute(tagIndex: tagIndex, tagFrame: tagFrame)
layoutInfoList[tagIndex].layoutAttribute = tagAttribute
}
}
}
}

func handleWhiteSpaceForLastRow() {
if isLastRow {
Expand Down
Empty file modified TagCellLayout.podspec 100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions TagCellLayout.xcodeproj/project.pbxproj 100644 → 100755
Expand Up @@ -279,6 +279,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.Ritesh.TagCellLayout;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "ff255666-24e2-4500-b9ea-19ca6b2df949";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -293,6 +294,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.Ritesh.TagCellLayout;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "ff255666-24e2-4500-b9ea-19ca6b2df949";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified TagCellLayout/AppDelegate.swift 100644 → 100755
Empty file.
Empty file modified TagCellLayout/Assets.xcassets/AppIcon.appiconset/Contents.json 100644 → 100755
Empty file.
Empty file modified TagCellLayout/Base.lproj/LaunchScreen.storyboard 100644 → 100755
Empty file.
18 changes: 11 additions & 7 deletions TagCellLayout/Base.lproj/Main.storyboard 100644 → 100755
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13526" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13524"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,12 +18,12 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="Ep7-BN-QcX">
<rect key="frame" x="0.0" y="64" width="600" height="536"/>
<color key="backgroundColor" red="0.97265625" green="0.97265625" blue="0.97265625" alpha="1" colorSpace="calibratedRGB"/>
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<color key="backgroundColor" red="0.97265625" green="0.97265625" blue="0.97265625" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<collectionViewLayout key="collectionViewLayout" id="NHW-no-Q6w" customClass="TagCellLayout" customModule="TagCellLayout" customModuleProvider="target"/>
<cells/>
<connections>
Expand All @@ -28,7 +32,7 @@
</connections>
</collectionView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Ep7-BN-QcX" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" constant="64" id="2ic-ZM-dbq"/>
<constraint firstItem="Ep7-BN-QcX" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="XEl-xz-2ft"/>
Expand Down
Empty file modified TagCellLayout/Info.plist 100644 → 100755
Empty file.
Empty file modified TagCellLayout/Readme_Resources/tag_cc.png 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified TagCellLayout/Readme_Resources/tag_ll.png 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified TagCellLayout/Readme_Resources/tag_rr.png 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions TagCellLayout/TagCollectionViewCell.swift 100644 → 100755
Expand Up @@ -16,9 +16,9 @@ class TagCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
if let tagView = tagView {
tagView.layer.cornerRadius = tagView.frame.size.height/2 - 2
tagView.layer.borderColor = UIColor.blueColor().CGColor
tagView.layer.borderColor = UIColor.blue.cgColor
tagView.layer.borderWidth = 3.0
}
}

}
}
15 changes: 9 additions & 6 deletions TagCellLayout/TagCollectionViewCell.xib 100644 → 100755
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10116" systemVersion="15B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13526" 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="10085"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13524"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand All @@ -16,13 +20,12 @@
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Tags" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="XTE-Bs-m9s">
<rect key="frame" x="2" y="10" width="138" height="38"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
<constraints>
<constraint firstAttribute="trailing" secondItem="XTE-Bs-m9s" secondAttribute="trailing" constant="2" id="MeS-8d-ODJ"/>
Expand Down
10 changes: 5 additions & 5 deletions TagCellLayout/ViewController.swift 100644 → 100755
Expand Up @@ -17,7 +17,7 @@ class ViewController: UIViewController, UICollectionViewDelegate, UICollectionVi
// Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(animated: Bool) {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
defaultSetup()

Expand Down Expand Up @@ -46,22 +46,22 @@ class ViewController: UIViewController, UICollectionViewDelegate, UICollectionVi

func defaultSetup() {
let nib = UINib(nibName: "TagCollectionViewCell", bundle: nil)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "TagCollectionViewCell")
collectionView?.register(nib, forCellWithReuseIdentifier: "TagCollectionViewCell")
}

//MARK: - UICollectionView Delegate/Datasource Methods

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier = "TagCollectionViewCell"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
return cell
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}

Expand Down

0 comments on commit 4ad1b0d

Please sign in to comment.