Skip to content

Commit

Permalink
Merge pull request #3 from ranesr/master
Browse files Browse the repository at this point in the history
Swift 4 Support
  • Loading branch information
joeydong-stripe authored Apr 11, 2018
2 parents bfc09c2 + 3e553e3 commit 604b575
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode8.3
osx_image: xcode9.2
before_install:
- ./scripts/install_dependencies.sh
script:
Expand Down
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "facebook/ios-snapshot-test-case" ~>2.1.1
github "uber/ios-snapshot-test-case" ~>3.0.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "facebook/ios-snapshot-test-case" "2.1.4"
github "uber/ios-snapshot-test-case" "3.0.0"
38 changes: 28 additions & 10 deletions DashboardUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,20 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0830;
LastUpgradeCheck = 0920;
ORGANIZATIONNAME = stripe;
TargetAttributes = {
96FA3F3E1D4A592C0045B6B9 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0830;
LastSwiftMigration = 0920;
};
96FA3F761D4A5BDC0045B6B9 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0830;
LastSwiftMigration = 0920;
};
96FA3FB41D4A5FE50045B6B9 = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0830;
LastSwiftMigration = 0920;
TestTargetID = 96FA3F761D4A5BDC0045B6B9;
};
};
Expand Down Expand Up @@ -357,14 +357,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -408,14 +414,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -460,7 +472,8 @@
PRODUCT_NAME = StripeDashboardUI;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -479,7 +492,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.stripe.DashboardUI;
PRODUCT_NAME = StripeDashboardUI;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -491,7 +505,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.stripe.DashboardUIExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -503,7 +518,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.stripe.DashboardUIExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -519,7 +535,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.stripe.DashboardUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DashboardUIExample.app/DashboardUIExample";
};
name = Debug;
Expand All @@ -536,7 +553,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.stripe.DashboardUITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DashboardUIExample.app/DashboardUIExample";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0830"
LastUpgradeVersion = "0920"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -20,12 +20,41 @@
ReferencedContainer = "container:DashboardUI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "96FA3FB41D4A5FE50045B6B9"
BuildableName = "DashboardUITests.xctest"
BlueprintName = "DashboardUITests"
ReferencedContainer = "container:DashboardUI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "96FA3F761D4A5BDC0045B6B9"
BuildableName = "DashboardUIExample.app"
BlueprintName = "DashboardUIExample"
ReferencedContainer = "container:DashboardUI.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +84,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
54 changes: 28 additions & 26 deletions DashboardUI/MoneyTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
var integerPart = amountString
var fractionalPart: String? = nil
if let decimalRange = amountString.range(of: self.decimalSeparator) {
integerPart = String(amountString.characters.prefix(upTo: decimalRange.lowerBound))
fractionalPart = String(amountString.characters.suffix(from: decimalRange.upperBound))
integerPart = String(amountString.prefix(upTo: decimalRange.lowerBound))
fractionalPart = String(amountString.suffix(from: decimalRange.upperBound))
}
if integerPart.characters.isEmpty {
if integerPart.isEmpty {
integerPart = self.zero
}
return (integerPart, fractionalPart)
Expand All @@ -203,24 +203,26 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
// not sure why this multiplier is necessary (baseline offset is in points),
// but it scales well for different font sizes.
let baselineOffset = baselinePoints*0.7
let smallAttributes = [
NSBaselineOffsetAttributeName: baselineOffset,
NSFontAttributeName: self.smallFont
] as [String : Any]
let smallAttributes: [NSAttributedStringKey : Any]?
smallAttributes = [
NSAttributedStringKey(rawValue: NSAttributedStringKey.baselineOffset.rawValue): baselineOffset,
NSAttributedStringKey.font: self.smallFont
]
var currencyAttributes = smallAttributes
currencyAttributes[NSForegroundColorAttributeName] = self.currencySymbolColor
currencyAttributes![NSAttributedStringKey.foregroundColor] = self.currencySymbolColor
var fractionalAttributes = smallAttributes
fractionalAttributes[NSForegroundColorAttributeName] = self.numberColor
let integerAttributes = [
NSFontAttributeName: self.largeFont,
NSForegroundColorAttributeName: self.numberColor
] as [String : Any]
fractionalAttributes![NSAttributedStringKey.foregroundColor] = self.numberColor
let integerAttributes: [NSAttributedStringKey : Any]?
integerAttributes = [
NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): self.largeFont,
NSAttributedStringKey.foregroundColor: self.numberColor
]
let string = NSMutableAttributedString(string: self.numberFormatter.currencySymbol,
attributes: currencyAttributes)
string.addAttribute(NSKernAttributeName, value: 4,
string.addAttribute(NSAttributedStringKey.kern, value: 4,
range: NSMakeRange(string.length - 1, 1))
let (integerPart, fractionalPart) = self.split(amountString)
if integerPart.characters.count > 0 {
if integerPart.count > 0 {
let number = NSDecimalNumber(string: integerPart.stp_sanitize(),
locale: self.locale)
let formatted = self.formatNumber(number,
Expand All @@ -233,7 +235,7 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
}
if let fractional = fractionalPart {
// using a space rather than kerning in order to make the caret resize
let spaceAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 10)]
let spaceAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 10)]
let space = NSAttributedString(string: " ", attributes: spaceAttributes)
string.append(space)
let fraction = NSAttributedString(string: fractional,
Expand All @@ -242,7 +244,7 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
string.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.length))
string.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, string.length))
return string
}

Expand Down Expand Up @@ -318,17 +320,17 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
// MARK: UITextViewDelegate
open func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var newRawString = self.rawString
let deleting = range.location == textView.text.characters.count - 1 && range.length == 1 && text == ""
let deleting = range.location == textView.text.count - 1 && range.length == 1 && text == ""
if deleting {
if newRawString.characters.count > 0 {
var lastIndex = newRawString.characters.index(before: newRawString.endIndex)
if newRawString.count > 0 {
var lastIndex = newRawString.index(before: newRawString.endIndex)
// delete over the decimal separator
if String(newRawString[lastIndex]) == self.decimalSeparator &&
newRawString.characters.count > 1 {
lastIndex = newRawString.characters.index(before: lastIndex)
newRawString.count > 1 {
lastIndex = newRawString.index(before: lastIndex)
}
newRawString = newRawString.substring(to: lastIndex)
if newRawString.characters.isEmpty {
newRawString = String(newRawString[..<lastIndex])
if newRawString.isEmpty {
newRawString = self.zero
}
}
Expand All @@ -341,7 +343,7 @@ open class MoneyTextField: UIControl, UITextViewDelegate, InternalTextViewDelega
let (integerPart, fractionalPart) = self.split(newRawString)
let sanitized = text.stp_sanitize()
if let fractional = fractionalPart {
if fractional.characters.count < self.numberFormatter.minimumFractionDigits {
if fractional.count < self.numberFormatter.minimumFractionDigits {
newRawString = integerPart + self.decimalSeparator + fractional + sanitized
}
}
Expand Down Expand Up @@ -414,7 +416,7 @@ private extension UIFont {
let string: NSString = "0"
let rect = string.boundingRect(with: CGSize.zero,
options: .usesDeviceMetrics,
attributes: [NSFontAttributeName: self],
attributes: [NSAttributedStringKey.font: self],
context: nil)
return rect.size.height
}
Expand Down
18 changes: 11 additions & 7 deletions DashboardUIExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15E65" 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="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina5_9" 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="13772"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,18 +19,18 @@
<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="812"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gWi-lz-i6A" customClass="MoneyTextField" customModule="StripeDashboardUI">
<rect key="frame" x="20" y="70" width="560" height="128"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<rect key="frame" x="16" y="94" width="343" height="128"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="128" id="RUF-eP-Bcp"/>
</constraints>
</view>
</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="gWi-lz-i6A" firstAttribute="trailing" secondItem="8bC-Xf-vdC" secondAttribute="trailingMargin" id="Utm-Kd-Cuf"/>
<constraint firstItem="gWi-lz-i6A" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="50" id="f4C-KO-vuq"/>
Expand Down
4 changes: 2 additions & 2 deletions DashboardUIExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class ViewController: UIViewController {
// Do any additional setup after loading the view, typically from a nib.
}

func tap() {
@objc func tap() {
let _ = self.moneyTextField.resignFirstResponder()
}

func amountChanged() {
@objc func amountChanged() {
print(self.moneyTextField.amountString)
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This is a collection of UI components used in the [Stripe Dashboard](https://itu
* Manual
* drag source files from the `DashboardUI` directory into your project.

Swift 3
Swift 4

## Components
### MoneyTextField
Expand Down
8 changes: 4 additions & 4 deletions StripeDashboardUI.podspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Pod::Spec.new do |s|
s.name = "StripeDashboardUI"
s.version = "0.2"
s.version = "0.3"
s.authors = { "Ben Guo" => "[email protected]",
"Jack Flintermann" => "[email protected]",
"Brian Dorfman" => "[email protected]" }
s.homepage = "https://github.com/stripe/ios-dashboard-ui"
s.summary = "UI components from the Stripe Dashboard iOS app"
s.source = { :git => "https://github.com/stripe/ios-dashboard-ui.git",
:tag => '0.2' }
:tag => '0.3' }
s.license = { :type => "MIT", :file => "LICENSE" }
s.platform = :ios, '8.0'
s.platform = :ios, '9.0'
s.source_files = "DashboardUI/*.swift"
s.requires_arc = true
s.ios.deployment_target = '8.0'
s.ios.deployment_target = '9.0'
s.ios.frameworks = ['UIKit', 'Foundation']
end
Loading

0 comments on commit 604b575

Please sign in to comment.