Skip to content

Commit

Permalink
[Swift 4] Support both Swift 4 and 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Tunius committed Oct 2, 2017
1 parent 49232d1 commit a5fe9c9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -653,7 +653,7 @@
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextField;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -664,7 +664,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SkyFloatingLabelTextFieldExample.app/SkyFloatingLabelTextFieldExample";
};
name = Debug;
Expand All @@ -676,7 +676,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = net.skyscanner.SkyFloatingLabelTextFieldTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SkyFloatingLabelTextFieldExample.app/SkyFloatingLabelTextFieldExample";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
Expand Down Expand Up @@ -56,6 +57,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
styles.errorColor = .purple

// Fonts
styles.font = .systemFont(ofSize: 14, weight: 1.0)
styles.placeholderFont = .systemFont(ofSize: 14, weight: 0.1)
#if swift(>=4.0)
styles.font = .systemFont(ofSize: 14, weight: UIFont.Weight(rawValue: 1.0))
styles.placeholderFont = .systemFont(ofSize: 14, weight: UIFont.Weight(0.1))
#else
styles.font = .systemFont(ofSize: 14, weight: 1.0)
styles.placeholderFont = .systemFont(ofSize: 14, weight: 0.1)
#endif

// Line
styles.lineHeight = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ class CustomizingColorsViewController: UIViewController {
// NOTE: For emojis to appear properly we need to set the color to white
// http://stackoverflow.com/a/38195951

let attributes: [String: Any] = [NSForegroundColorAttributeName: UIColor.white]
var attributes: [String: Any] = [:]

#if swift(>=4.0)
attributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
#else
attributes = [NSForegroundColorAttributeName: UIColor.white]
#endif

selectedTitleColorControl?.setTitleTextAttributes(attributes, for: .selected)
titleColorControl?.setTitleTextAttributes(attributes, for: .selected)
textColorControl?.setTitleTextAttributes(attributes, for: .selected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,21 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
floatingLabelTextField.placeholderColor = customColor

// then
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSForegroundColorAttributeName, at: 0, effectiveRange: &fullRange
) as? UIColor,
#if swift(>=4.0)
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSAttributedStringKey.foregroundColor, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
#else
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSForegroundColorAttributeName, at: 0, effectiveRange: &fullRange
) as? UIColor,
customColor
)
)
#endif
}

func test_whenSettingTitleColor_thenTitleLabelTextColorIsChangedToThisColor() {
Expand Down Expand Up @@ -151,14 +160,25 @@ class SkyFloatingLabelTextFieldTests: XCTestCase { // swiftlint:disable:this typ
floatingLabelTextField.placeholderFont = customFont

// then
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSFontAttributeName,
at: 0,
effectiveRange: &fullRange
) as? UIFont,
customFont
)
#if swift(>=4.0)
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSAttributedStringKey.font,
at: 0,
effectiveRange: &fullRange
) as? UIFont,
customFont
)
#else
XCTAssertEqual(
floatingLabelTextField.attributedPlaceholder!.attribute(
NSFontAttributeName,
at: 0,
effectiveRange: &fullRange
) as? UIFont,
customFont
)
#endif
}

func test_whenSettingSelectedLineColor_withTextfieldBeingSelected_thenLineViewBackgroundColorIsChangedToThisColor() { // swiftlint:disable:this line_length
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions Sources/SkyFloatingLabelTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
// MARK: Animation timing

/// The value of the title appearing duration
dynamic open var titleFadeInDuration: TimeInterval = 0.2
@objc dynamic open var titleFadeInDuration: TimeInterval = 0.2
/// The value of the title disappearing duration
dynamic open var titleFadeOutDuration: TimeInterval = 0.3
@objc dynamic open var titleFadeOutDuration: TimeInterval = 0.3

// MARK: Colors

Expand All @@ -67,23 +67,32 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
}

/// A UIFont value that determines text color of the placeholder label
dynamic open var placeholderFont: UIFont? {
@objc dynamic open var placeholderFont: UIFont? {
didSet {
updatePlaceholder()
}
}

fileprivate func updatePlaceholder() {
if let placeholder = placeholder, let font = placeholderFont ?? font {
#if swift(>=4.0)
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [
NSAttributedStringKey.foregroundColor: placeholderColor, NSAttributedStringKey.font: font
]
)
#else
attributedPlaceholder = NSAttributedString(
string: placeholder,
attributes: [NSForegroundColorAttributeName: placeholderColor, NSFontAttributeName: font]
)
#endif
}
}

/// A UIFont value that determines the text font of the title label
dynamic open var titleFont: UIFont = .systemFont(ofSize: 13) {
@objc dynamic open var titleFont: UIFont = .systemFont(ofSize: 13) {
didSet {
updateTitleLabel()
}
Expand Down Expand Up @@ -289,7 +298,7 @@ open class SkyFloatingLabelTextField: UITextField { // swiftlint:disable:this ty
/**
Invoked when the editing state of the textfield changes. Override to respond to this change.
*/
open func editingChanged() {
@objc open func editingChanged() {
updateControl(true)
updateTitleLabel(true)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SkyFloatingLabelTextFieldWithIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField {
open var iconLabel: UILabel!

/// A UIFont value that determines the font that the icon is using
dynamic open var iconFont: UIFont? {
@objc dynamic open var iconFont: UIFont? {
didSet {
iconLabel?.font = iconFont
}
Expand Down

0 comments on commit a5fe9c9

Please sign in to comment.