Skip to content

Commit 4df6c1c

Browse files
author
Kyle Begeman
authored
Merge pull request #3 from kylebegeman/protocol-config
Configuration is now represented with the protocol InlineTagConfigurable
2 parents 45b27bd + c0a764b commit 4df6c1c

8 files changed

Lines changed: 183 additions & 109 deletions

File tree

Example/ViewController.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ class ViewController: UIViewController, InlineTagControllerDelegate {
1717
super.viewDidLoad()
1818
// Do any additional setup after loading the view, typically from a nib.
1919

20+
tagController.setConfiguration(CustomConfiguration())
2021
tagController.tagDelegate = self
21-
tagController.setPlaceholderText(text: "Enter new tags...")
22-
23-
InlineTagControllerConfiguration.itemValidation = InlineTagControllerValidation.testEmptiness
24-
InlineTagControllerConfiguration.numberOfTags = .quantity(5) // .unlimited also available
2522
}
2623

2724
func inlineTagController(_ controller: InlineTagController, didFinishEditing text: String) {
@@ -34,3 +31,18 @@ class ViewController: UIViewController, InlineTagControllerDelegate {
3431

3532
}
3633

34+
class CustomConfiguration: InlineTagConfigurable {
35+
var backgroundColor: ColorCollection = (view: UIColor.black, edit: UIColor.white, invalid: UIColor.blue, placeholder: UIColor.green)
36+
var radius: ValueCollection = (view: 10.0, edit: 10.0, invalid: 10.0)
37+
38+
39+
var font: FontCollection {
40+
let tagFont = UIFont.systemFont(ofSize: 14.0)
41+
return (view: tagFont, edit: tagFont, invalid: tagFont, placeholder: tagFont)
42+
}
43+
44+
var cellHeight: Float = 20.0
45+
var inset: UIEdgeInsets = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 2.0, right: 2.0)
46+
47+
// Note: Any properties of 'InlineTagConfigurable' not implemented will use default values provided by the framework.
48+
}

InlineTagController.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
27BD79251F13F1DE0018832A /* InlineTagController.h in Headers */ = {isa = PBXBuildFile; fileRef = 27BD79231F13F1DE0018832A /* InlineTagController.h */; settings = {ATTRIBUTES = (Public, ); }; };
1111
27BD79321F13F2060018832A /* InlineTagCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD792D1F13F2060018832A /* InlineTagCell.swift */; };
1212
27BD79331F13F2060018832A /* InlineTagController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD792E1F13F2060018832A /* InlineTagController.swift */; };
13-
27BD79341F13F2060018832A /* InlineTagControllerConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD792F1F13F2060018832A /* InlineTagControllerConfiguration.swift */; };
13+
27BD79341F13F2060018832A /* InlineTagConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD792F1F13F2060018832A /* InlineTagConfigurable.swift */; };
1414
27BD79351F13F2060018832A /* InlineTagControllerFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD79301F13F2060018832A /* InlineTagControllerFlowLayout.swift */; };
1515
27BD79361F13F2060018832A /* InlineTagControllerValidation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD79311F13F2060018832A /* InlineTagControllerValidation.swift */; };
1616
27BD793E1F13F2680018832A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27BD793D1F13F2680018832A /* AppDelegate.swift */; };
@@ -26,7 +26,7 @@
2626
27BD79241F13F1DE0018832A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2727
27BD792D1F13F2060018832A /* InlineTagCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagCell.swift; path = /Users/kyle/Desktop/InlineTagController/Sources/InlineTagCell.swift; sourceTree = "<absolute>"; };
2828
27BD792E1F13F2060018832A /* InlineTagController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagController.swift; path = /Users/kyle/Desktop/InlineTagController/InlineTagController/../Sources/InlineTagController.swift; sourceTree = "<absolute>"; };
29-
27BD792F1F13F2060018832A /* InlineTagControllerConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagControllerConfiguration.swift; path = /Users/kyle/Desktop/InlineTagController/InlineTagController/../Sources/InlineTagControllerConfiguration.swift; sourceTree = "<absolute>"; };
29+
27BD792F1F13F2060018832A /* InlineTagConfigurable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagConfigurable.swift; path = /Users/kyle/Desktop/InlineTagController/Sources/InlineTagConfigurable.swift; sourceTree = "<absolute>"; };
3030
27BD79301F13F2060018832A /* InlineTagControllerFlowLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagControllerFlowLayout.swift; path = /Users/kyle/Desktop/InlineTagController/InlineTagController/../Sources/InlineTagControllerFlowLayout.swift; sourceTree = "<absolute>"; };
3131
27BD79311F13F2060018832A /* InlineTagControllerValidation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = InlineTagControllerValidation.swift; path = /Users/kyle/Desktop/InlineTagController/InlineTagController/../Sources/InlineTagControllerValidation.swift; sourceTree = "<absolute>"; };
3232
27BD793B1F13F2680018832A /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -97,7 +97,7 @@
9797
children = (
9898
27BD792D1F13F2060018832A /* InlineTagCell.swift */,
9999
27BD792E1F13F2060018832A /* InlineTagController.swift */,
100-
27BD792F1F13F2060018832A /* InlineTagControllerConfiguration.swift */,
100+
27BD792F1F13F2060018832A /* InlineTagConfigurable.swift */,
101101
27BD79301F13F2060018832A /* InlineTagControllerFlowLayout.swift */,
102102
27BD79311F13F2060018832A /* InlineTagControllerValidation.swift */,
103103
);
@@ -234,7 +234,7 @@
234234
buildActionMask = 2147483647;
235235
files = (
236236
27BD79321F13F2060018832A /* InlineTagCell.swift in Sources */,
237-
27BD79341F13F2060018832A /* InlineTagControllerConfiguration.swift in Sources */,
237+
27BD79341F13F2060018832A /* InlineTagConfigurable.swift in Sources */,
238238
27BD79331F13F2060018832A /* InlineTagController.swift in Sources */,
239239
27BD79351F13F2060018832A /* InlineTagControllerFlowLayout.swift in Sources */,
240240
27BD79361F13F2060018832A /* InlineTagControllerValidation.swift in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "Yes"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
filePath = "Sources/InlineTagController.swift"
13+
timestampString = "521582474.518573"
14+
startingColumnNumber = "9223372036854775807"
15+
endingColumnNumber = "9223372036854775807"
16+
startingLineNumber = "108"
17+
endingLineNumber = "108"
18+
landmarkName = "configure()"
19+
landmarkType = "7">
20+
</BreakpointContent>
21+
</BreakpointProxy>
22+
</Breakpoints>
23+
</Bucket>

Sources/InlineTagCell.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ class InlineTagCell: UICollectionViewCell, UITextFieldDelegate {
8484
self.textField.textAlignment = .center
8585
self.textField.contentMode = .left
8686

87-
self.textField.keyboardType = InlineTagControllerConfiguration.keyboardType
88-
self.textField.returnKeyType = InlineTagControllerConfiguration.returnKey
89-
self.textField.autocapitalizationType = InlineTagControllerConfiguration.autoCapitalization
90-
self.textField.autocorrectionType = InlineTagControllerConfiguration.autoCorrection
87+
self.textField.keyboardType = TagConfig.keyboardType
88+
self.textField.returnKeyType = TagConfig.returnKey
89+
self.textField.autocapitalizationType = TagConfig.autoCapitalization
90+
self.textField.autocorrectionType = TagConfig.autoCorrection
9191

9292
set(mode: .view)
9393
}
@@ -121,28 +121,28 @@ class InlineTagCell: UICollectionViewCell, UITextFieldDelegate {
121121

122122
switch m {
123123
case .edit:
124-
textField.backgroundColor = InlineTagControllerConfiguration.editBackgroundColor
125-
textField.font = InlineTagControllerConfiguration.editFont
126-
textField.textColor = InlineTagControllerConfiguration.editFontColor
124+
textField.backgroundColor = TagConfig.backgroundColor.edit
125+
textField.font = TagConfig.font.edit
126+
textField.textColor = TagConfig.fontColor.edit
127127

128-
self.backgroundColor = InlineTagControllerConfiguration.editBackgroundColor
129-
self.layer.cornerRadius = CGFloat(InlineTagControllerConfiguration.editCornerRadius)
128+
self.backgroundColor = TagConfig.backgroundColor.edit
129+
self.layer.cornerRadius = TagConfig.radius.edit
130130

131131
case .view:
132-
textField.backgroundColor = InlineTagControllerConfiguration.viewBackgroundColor
133-
textField.font = InlineTagControllerConfiguration.viewFont
134-
textField.textColor = InlineTagControllerConfiguration.viewFontColor
132+
textField.backgroundColor = TagConfig.backgroundColor.view
133+
textField.font = TagConfig.font.view
134+
textField.textColor = TagConfig.fontColor.view
135135

136-
self.backgroundColor = InlineTagControllerConfiguration.viewBackgroundColor
137-
self.layer.cornerRadius = CGFloat(InlineTagControllerConfiguration.viewCornerRadius)
136+
self.backgroundColor = TagConfig.backgroundColor.view
137+
self.layer.cornerRadius = TagConfig.radius.view
138138

139139
case .invalid:
140-
textField.backgroundColor = InlineTagControllerConfiguration.invalidBackgroundColor
141-
textField.font = InlineTagControllerConfiguration.invalidFont
142-
textField.textColor = InlineTagControllerConfiguration.invalidFontColor
140+
textField.backgroundColor = TagConfig.backgroundColor.invalid
141+
textField.font = TagConfig.font.invalid
142+
textField.textColor = TagConfig.fontColor.invalid
143143

144-
self.backgroundColor = InlineTagControllerConfiguration.invalidBackgroundColor
145-
self.layer.cornerRadius = CGFloat(InlineTagControllerConfiguration.invalidCornerRadius)
144+
self.backgroundColor = TagConfig.backgroundColor.invalid
145+
self.layer.cornerRadius = TagConfig.radius.invalid
146146
}
147147

148148
self.mode = mode
@@ -159,7 +159,7 @@ class InlineTagCell: UICollectionViewCell, UITextFieldDelegate {
159159
extension InlineTagCell {
160160

161161
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
162-
if string == " " && InlineTagControllerConfiguration.skipOnWhitespace && InlineTagControllerValidation.isValid(text: self.textField.text) {
162+
if string == " " && TagConfig.skipOnWhitespace && InlineTagControllerValidation.isValid(text: self.textField.text) {
163163
self.delegate?.createAndSwitchToNewCell(cell: self)
164164
} else if string == "" && textField.text == "" {
165165
self.delegate?.shouldDeleteCellInFrontOfCell(cell: self)
@@ -171,7 +171,7 @@ extension InlineTagCell {
171171
}
172172

173173
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
174-
if InlineTagControllerConfiguration.skipOnReturnKey {
174+
if TagConfig.skipOnReturnKey {
175175
if !InlineTagControllerValidation.isValid(text: textField.text) {
176176
return false
177177
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// InlineTagControllerConfiguration.swift
3+
// Testing
4+
//
5+
// Created by Kyle Begeman on 7/10/17.
6+
// Copyright © 2017 Kyle Begeman. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import UIKit
11+
12+
internal class Config {
13+
static let instance = Config()
14+
private init() {
15+
configuration = DefaultConfiguration()
16+
}
17+
18+
var configuration: InlineTagConfigurable
19+
func set(config: InlineTagConfigurable) {
20+
self.configuration = config
21+
}
22+
}
23+
24+
public typealias ColorCollection = (view: UIColor, edit: UIColor, invalid: UIColor, placeholder: UIColor?)
25+
public typealias FontCollection = (view: UIFont, edit: UIFont, invalid: UIFont, placeholder: UIFont)
26+
public typealias ValueCollection = (view: CGFloat, edit: CGFloat, invalid: CGFloat)
27+
28+
public enum NumberOfTags {
29+
case unlimited
30+
case quantity(Int)
31+
}
32+
33+
public class DefaultConfiguration: InlineTagConfigurable {}
34+
public let TagConfig = Config.instance.configuration
35+
36+
public protocol InlineTagConfigurable {
37+
var backgroundColor: ColorCollection { get }
38+
var fontColor: ColorCollection { get }
39+
var font: FontCollection { get }
40+
var radius: ValueCollection { get }
41+
42+
var cellHeight: Float { get }
43+
var inset: UIEdgeInsets { get }
44+
var interitemSpacing: CGFloat { get }
45+
var lineSpacing: CGFloat { get }
46+
var keyboardType: UIKeyboardType { get }
47+
var returnKey: UIReturnKeyType { get }
48+
var autoCapitalization: UITextAutocapitalizationType { get }
49+
var autoCorrection: UITextAutocorrectionType { get }
50+
var skipOnWhitespace: Bool { get }
51+
var skipOnReturnKey: Bool { get }
52+
var placeholderText: String { get }
53+
var numberOfTags: NumberOfTags { get }
54+
var itemValidation: Validation? { get }
55+
}
56+
57+
extension InlineTagConfigurable {
58+
public var backgroundColor: ColorCollection {
59+
return (view: UIColor.blue, edit: UIColor.white, invalid: UIColor.red, placeholder: nil)
60+
}
61+
public var fontColor: ColorCollection {
62+
return (view: UIColor.white, edit: UIColor.darkText, invalid: UIColor.white, placeholder: UIColor.gray)
63+
}
64+
public var radius: ValueCollection {
65+
return (view: 8.0, edit: 8.0, invalid: 8.0)
66+
}
67+
public var font: FontCollection {
68+
let font = UIFont.systemFont(ofSize: 12.0, weight: UIFontWeightMedium)
69+
let phFont = UIFont.systemFont(ofSize: 12.0, weight: UIFontWeightLight)
70+
return (view: font, edit: font, invalid: font, placeholder: phFont)
71+
}
72+
73+
public var cellHeight: Float { return 20.0 }
74+
public var inset: UIEdgeInsets { return UIEdgeInsets(top: 2, left: 4, bottom: 2, right: 4) }
75+
public var interitemSpacing: CGFloat { return 5.0 }
76+
public var lineSpacing: CGFloat { return 5.0 }
77+
public var keyboardType: UIKeyboardType { return .default }
78+
public var returnKey: UIReturnKeyType { return .done }
79+
public var autoCapitalization: UITextAutocapitalizationType { return .none }
80+
public var autoCorrection: UITextAutocorrectionType { return .no }
81+
public var skipOnWhitespace: Bool { return true }
82+
public var skipOnReturnKey: Bool { return true }
83+
public var placeholderText: String { return "Add tags..." }
84+
public var numberOfTags: NumberOfTags { return .unlimited }
85+
public var itemValidation: Validation? { return InlineTagControllerValidation.testEmptiness }
86+
}

0 commit comments

Comments
 (0)