Skip to content

Commit 04aed4c

Browse files
committed
Convert to Swift 4 syntax
* Use NSAttributedStringKey in string attributes * Use String slicing subscript with the PartialRangeUpTo operator
1 parent 5b9c92a commit 04aed4c

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

Example/CustomizedTokenField.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ extension ICTokenField {
6666
attributedPlaceholder = NSAttributedString(
6767
string: String(describing: type(of: self)),
6868
attributes: [
69-
NSForegroundColorAttributeName: UIColor.white.withAlphaComponent(0.5),
70-
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)
69+
.foregroundColor: UIColor.white.withAlphaComponent(0.5),
70+
.font: UIFont.boldSystemFont(ofSize: 14)
7171
]
7272
)
7373

7474
normalTokenAttributes = [
75-
NSForegroundColorAttributeName: UIColor.white,
76-
NSBackgroundColorAttributeName: UIColor.white.withAlphaComponent(0.25),
77-
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)
75+
.foregroundColor: UIColor.white,
76+
.backgroundColor: UIColor.white.withAlphaComponent(0.25),
77+
.font: UIFont.boldSystemFont(ofSize: 14)
7878
]
7979

8080
highlightedTokenAttributes = [
81-
NSForegroundColorAttributeName: UIColor(red:0.8, green:0.32, blue:0.24, alpha:1),
82-
NSBackgroundColorAttributeName: UIColor.white,
83-
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 14)
81+
.foregroundColor: UIColor(red:0.8, green:0.32, blue:0.24, alpha:1),
82+
.backgroundColor: UIColor.white,
83+
.font: UIFont.boldSystemFont(ofSize: 14)
8484
]
8585
}
8686

Example/StoryboardViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class StoryboardViewController: UITableViewController {
3232
@IBOutlet weak var tokenField: ICTokenField! {
3333
didSet {
3434
tokenField.normalTokenAttributes = [
35-
NSForegroundColorAttributeName: UIColor.white,
36-
NSBackgroundColorAttributeName: UIColor.white.withAlphaComponent(0.25)
35+
.foregroundColor: UIColor.white,
36+
.backgroundColor: UIColor.white.withAlphaComponent(0.25)
3737
]
3838

3939
tokenField.highlightedTokenAttributes = [
40-
NSForegroundColorAttributeName: UIColor.darkGray,
41-
NSBackgroundColorAttributeName: UIColor.white
40+
.foregroundColor: UIColor.darkGray,
41+
.backgroundColor: UIColor.white
4242
]
4343
}
4444
}

ICInputAccessory.xcodeproj/project.pbxproj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,18 @@
304304
TargetAttributes = {
305305
B53376871F4436D000230739 = {
306306
CreatedOnToolsVersion = 8.3.3;
307+
LastSwiftMigration = 0900;
307308
ProvisioningStyle = Automatic;
308309
};
309310
B533769A1F4436D000230739 = {
310311
CreatedOnToolsVersion = 8.3.3;
311-
LastSwiftMigration = 0830;
312+
LastSwiftMigration = 0900;
312313
ProvisioningStyle = Automatic;
313314
TestTargetID = B53376871F4436D000230739;
314315
};
315316
B56BC4291C89A7EA00C20AD6 = {
316317
CreatedOnToolsVersion = 7.2.1;
317-
LastSwiftMigration = 0830;
318+
LastSwiftMigration = 0900;
318319
};
319320
};
320321
};
@@ -509,7 +510,7 @@
509510
PRODUCT_BUNDLE_IDENTIFIER = com.polydice.ICInputAccessory.Example;
510511
PRODUCT_NAME = "$(TARGET_NAME)";
511512
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
512-
SWIFT_VERSION = 3.0;
513+
SWIFT_VERSION = 4.0;
513514
};
514515
name = Debug;
515516
};
@@ -528,7 +529,7 @@
528529
PRODUCT_BUNDLE_IDENTIFIER = com.polydice.ICInputAccessory.Example;
529530
PRODUCT_NAME = "$(TARGET_NAME)";
530531
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
531-
SWIFT_VERSION = 3.0;
532+
SWIFT_VERSION = 4.0;
532533
};
533534
name = Release;
534535
};
@@ -549,7 +550,7 @@
549550
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
550551
SWIFT_OBJC_BRIDGING_HEADER = "";
551552
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
552-
SWIFT_VERSION = 3.0;
553+
SWIFT_VERSION = 4.0;
553554
TEST_TARGET_NAME = Example;
554555
};
555556
name = Debug;
@@ -570,7 +571,7 @@
570571
PRODUCT_NAME = "$(TARGET_NAME)";
571572
SWIFT_OBJC_BRIDGING_HEADER = "";
572573
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
573-
SWIFT_VERSION = 3.0;
574+
SWIFT_VERSION = 4.0;
574575
TEST_TARGET_NAME = Example;
575576
};
576577
name = Release;
@@ -689,7 +690,7 @@
689690
PRODUCT_NAME = ICInputAccessory;
690691
SKIP_INSTALL = YES;
691692
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
692-
SWIFT_VERSION = 3.0;
693+
SWIFT_VERSION = 4.0;
693694
};
694695
name = Debug;
695696
};
@@ -710,7 +711,7 @@
710711
PRODUCT_NAME = ICInputAccessory;
711712
SKIP_INSTALL = YES;
712713
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
713-
SWIFT_VERSION = 3.0;
714+
SWIFT_VERSION = 4.0;
714715
};
715716
name = Release;
716717
};

Source/TokenField/ICBackspaceTextField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ICBackspaceTextField: UITextField {
5656

5757
// MARK: - UITextField
5858

59-
func keyboardInputShouldDelete(_ textField: UITextField) -> Bool {
59+
@objc func keyboardInputShouldDelete(_ textField: UITextField) -> Bool {
6060
return backspaceDelegate?.textFieldShouldDelete(self) ?? true
6161
}
6262

Source/TokenField/ICToken.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ class ICToken: UIView {
4141
}
4242
}
4343

44-
var normalTextAttributes: [String : NSObject] = [
45-
NSForegroundColorAttributeName: UIColor(red:0.14, green:0.38, blue:0.95, alpha:1),
46-
NSBackgroundColorAttributeName: UIColor.clear
44+
var normalTextAttributes: [NSAttributedStringKey: NSObject] = [
45+
.foregroundColor: UIColor(red:0.14, green:0.38, blue:0.95, alpha:1),
46+
.backgroundColor: UIColor.clear
4747
] {
4848
didSet {
4949
if !isHighlighted { updateTextLabel() }
50-
delimiterLabel.textColor = self.normalTextAttributes[NSForegroundColorAttributeName] as? UIColor
50+
delimiterLabel.textColor = self.normalTextAttributes[.foregroundColor] as? UIColor
5151
}
5252
}
5353

54-
var highlightedTextAttributes: [String: NSObject] = [
55-
NSForegroundColorAttributeName: UIColor.white,
56-
NSBackgroundColorAttributeName: UIColor(red:0.14, green:0.38, blue:0.95, alpha:1)
54+
var highlightedTextAttributes: [NSAttributedStringKey: NSObject] = [
55+
.foregroundColor: UIColor.white,
56+
.backgroundColor: UIColor(red:0.14, green:0.38, blue:0.95, alpha:1)
5757
] {
5858
didSet {
5959
if isHighlighted { updateTextLabel() }
@@ -64,16 +64,16 @@ class ICToken: UIView {
6464

6565
private(set) lazy var delimiterLabel: UILabel = {
6666
let _label = UILabel()
67-
_label.textColor = self.normalTextAttributes[NSForegroundColorAttributeName] as? UIColor
67+
_label.textColor = self.normalTextAttributes[.foregroundColor] as? UIColor
6868
_label.textAlignment = .right
6969
return _label
7070
}()
7171

7272
private(set) lazy var textLabel: UILabel = {
7373
let _label = ICInsetLabel(contentEdgeInsets: UIEdgeInsets(top: 3, left: 5, bottom: 3, right: 5), cornerRadius: .constant(3))
7474
_label.textAlignment = .center
75-
_label.textColor = self.normalTextAttributes[NSForegroundColorAttributeName] as? UIColor
76-
_label.backgroundColor = self.normalTextAttributes[NSBackgroundColorAttributeName] as? UIColor
75+
_label.textColor = self.normalTextAttributes[.foregroundColor] as? UIColor
76+
_label.backgroundColor = self.normalTextAttributes[.backgroundColor] as? UIColor
7777
_label.numberOfLines = 1
7878
return _label
7979
}()
@@ -90,7 +90,12 @@ class ICToken: UIView {
9090
setUpSubviews()
9191
}
9292

93-
convenience init(text: String, delimiter: String = ",", normalAttributes: [String: NSObject]? = nil, highlightedAttributes: [String: NSObject]? = nil) {
93+
convenience init(
94+
text: String,
95+
delimiter: String = ",",
96+
normalAttributes: [NSAttributedStringKey: NSObject]? = nil,
97+
highlightedAttributes: [NSAttributedStringKey: NSObject]? = nil
98+
) {
9499
self.init()
95100
if let attributes = normalAttributes { normalTextAttributes = attributes }
96101
if let attributes = highlightedAttributes { highlightedTextAttributes = attributes }
@@ -105,15 +110,15 @@ class ICToken: UIView {
105110

106111
private func updateTextLabel() {
107112
var attributes = isHighlighted ? highlightedTextAttributes : normalTextAttributes
108-
if let color = attributes[NSBackgroundColorAttributeName] as? UIColor {
113+
if let color = attributes[.backgroundColor] as? UIColor {
109114
textLabel.backgroundColor = color
110115
}
111116
// Avoid overlapped translucent background colors
112-
attributes[NSBackgroundColorAttributeName] = nil
117+
attributes[.backgroundColor] = nil
113118
textLabel.attributedText = NSAttributedString(string: text, attributes: attributes)
114119

115-
delimiterLabel.textColor = normalTextAttributes[NSForegroundColorAttributeName] as? UIColor
116-
delimiterLabel.font = normalTextAttributes[NSFontAttributeName] as? UIFont
120+
delimiterLabel.textColor = normalTextAttributes[.foregroundColor] as? UIColor
121+
delimiterLabel.font = normalTextAttributes[.font] as? UIFont
117122
}
118123

119124
private func setUpSubviews() {

Source/TokenField/ICTokenField.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
9292
if let text = newValue {
9393
attributedPlaceholder = NSAttributedString(
9494
string: text,
95-
attributes: [NSForegroundColorAttributeName: UIColor(red: 0.78, green: 0.78, blue: 0.80, alpha: 0.9)]
95+
attributes: [.foregroundColor: UIColor(red: 0.78, green: 0.78, blue: 0.80, alpha: 0.9)]
9696
)
9797
} else {
9898
attributedPlaceholder = nil
@@ -114,22 +114,22 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
114114
if placeholderLabel.superview != nil { return }
115115
insertSubview(placeholderLabel, belowSubview: scrollView)
116116
placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
117-
placeholderLabel.setContentHuggingPriority(UILayoutPriorityDefaultLow - 1, for: .horizontal)
117+
placeholderLabel.setContentHuggingPriority(UILayoutPriority(rawValue: UILayoutPriority.RawValue(Int(UILayoutPriority.defaultLow.rawValue) - 1)), for: .horizontal)
118118
addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .leading, relatedBy: .equal, toItem: scrollView, attribute: .leading, multiplier: 1, constant: 0))
119119
addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .trailing, relatedBy: .greaterThanOrEqual, toItem: scrollView, attribute: .trailing, multiplier: 1, constant: 10))
120120
addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
121121
}
122122
}
123123

124124
/// Customized attributes for tokens in the normal state, e.g. `NSFontAttributeName` and `NSForegroundColorAttributeName`.
125-
public var normalTokenAttributes: [String: NSObject]? {
125+
public var normalTokenAttributes: [NSAttributedStringKey: NSObject]? {
126126
didSet {
127127
tokens.forEach { $0.normalTextAttributes = normalTokenAttributes ?? [:] }
128128
}
129129
}
130130

131131
/// Customized attributes for tokens in the highlighted state.
132-
public var highlightedTokenAttributes: [String: NSObject]? {
132+
public var highlightedTokenAttributes: [NSAttributedStringKey: NSObject]? {
133133
didSet {
134134
tokens.forEach { $0.highlightedTextAttributes = normalTokenAttributes ?? [:] }
135135
}
@@ -301,7 +301,7 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
301301
}
302302

303303
let index = text.index(text.endIndex, offsetBy: -delimiter.characters.count)
304-
let newText = text.substring(to: index)
304+
let newText = String(text[..<index])
305305

306306
if !newText.isEmpty && newText != delimiter && (delegate?.tokenField?(self, shouldCompleteText: newText) ?? true) {
307307
tokens.append(customizedToken(with: newText))
@@ -344,7 +344,7 @@ open class ICTokenField: UIView, UITextFieldDelegate, ICBackspaceTextFieldDelega
344344
// textField(_:shouldChangeCharactersIn:replacementString:) is skipped when the delete key is pressed.
345345
// Notify the delegate of the changed input text manually.
346346
let index = text.index(text.endIndex, offsetBy: -1)
347-
delegate?.tokenField?(self, didChangeInputText: text.substring(to: index))
347+
delegate?.tokenField?(self, didChangeInputText: String(text[..<index]))
348348
}
349349

350350
return true

0 commit comments

Comments
 (0)