diff --git a/DateTextField.xcodeproj/project.pbxproj b/DateTextField.xcodeproj/project.pbxproj new file mode 100644 index 0000000..93c87e8 --- /dev/null +++ b/DateTextField.xcodeproj/project.pbxproj @@ -0,0 +1,340 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + A1B754F91EBDAFE200472852 /* DateTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B754F81EBDAFE200472852 /* DateTextField.swift */; }; + A1B755041EBDAFEE00472852 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B754FB1EBDAFEE00472852 /* AppDelegate.swift */; }; + A1B755051EBDAFEE00472852 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A1B754FC1EBDAFEE00472852 /* Assets.xcassets */; }; + A1B755061EBDAFEE00472852 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A1B754FD1EBDAFEE00472852 /* LaunchScreen.storyboard */; }; + A1B755071EBDAFEE00472852 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A1B754FF1EBDAFEE00472852 /* Main.storyboard */; }; + A1B755081EBDAFEE00472852 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = A1B755011EBDAFEE00472852 /* Info.plist */; }; + A1B755091EBDAFEE00472852 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B755031EBDAFEE00472852 /* ViewController.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + A12016091E7E370000D1E685 /* DateTextField.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DateTextField.app; sourceTree = BUILT_PRODUCTS_DIR; }; + A1B754F81EBDAFE200472852 /* DateTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DateTextField.swift; sourceTree = ""; }; + A1B754FB1EBDAFEE00472852 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + A1B754FC1EBDAFEE00472852 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + A1B754FE1EBDAFEE00472852 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + A1B755001EBDAFEE00472852 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + A1B755011EBDAFEE00472852 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A1B755031EBDAFEE00472852 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + A12016061E7E370000D1E685 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + A12016001E7E370000D1E685 = { + isa = PBXGroup; + children = ( + A120160B1E7E370000D1E685 /* DateTextField */, + A120160A1E7E370000D1E685 /* Products */, + ); + sourceTree = ""; + }; + A120160A1E7E370000D1E685 /* Products */ = { + isa = PBXGroup; + children = ( + A12016091E7E370000D1E685 /* DateTextField.app */, + ); + name = Products; + sourceTree = ""; + }; + A120160B1E7E370000D1E685 /* DateTextField */ = { + isa = PBXGroup; + children = ( + A1B754FA1EBDAFEE00472852 /* Demo */, + A1B754F71EBDAFE200472852 /* DateTextField */, + ); + path = DateTextField; + sourceTree = ""; + }; + A1B754F71EBDAFE200472852 /* DateTextField */ = { + isa = PBXGroup; + children = ( + A1B754F81EBDAFE200472852 /* DateTextField.swift */, + ); + name = DateTextField; + sourceTree = ""; + }; + A1B754FA1EBDAFEE00472852 /* Demo */ = { + isa = PBXGroup; + children = ( + A1B754FB1EBDAFEE00472852 /* AppDelegate.swift */, + A1B754FC1EBDAFEE00472852 /* Assets.xcassets */, + A1B754FD1EBDAFEE00472852 /* LaunchScreen.storyboard */, + A1B754FF1EBDAFEE00472852 /* Main.storyboard */, + A1B755011EBDAFEE00472852 /* Info.plist */, + A1B755021EBDAFEE00472852 /* untitled folder */, + A1B755031EBDAFEE00472852 /* ViewController.swift */, + ); + path = Demo; + sourceTree = SOURCE_ROOT; + }; + A1B755021EBDAFEE00472852 /* untitled folder */ = { + isa = PBXGroup; + children = ( + ); + path = "untitled folder"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + A12016081E7E370000D1E685 /* DateTextField */ = { + isa = PBXNativeTarget; + buildConfigurationList = A120161B1E7E370100D1E685 /* Build configuration list for PBXNativeTarget "DateTextField" */; + buildPhases = ( + A12016051E7E370000D1E685 /* Sources */, + A12016061E7E370000D1E685 /* Frameworks */, + A12016071E7E370000D1E685 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = DateTextField; + productName = DateTextField; + productReference = A12016091E7E370000D1E685 /* DateTextField.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + A12016011E7E370000D1E685 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0820; + LastUpgradeCheck = 0820; + ORGANIZATIONNAME = "Beau Nouvelle"; + TargetAttributes = { + A12016081E7E370000D1E685 = { + CreatedOnToolsVersion = 8.2.1; + DevelopmentTeam = RCFBK97QU7; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = A12016041E7E370000D1E685 /* Build configuration list for PBXProject "DateTextField" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = A12016001E7E370000D1E685; + productRefGroup = A120160A1E7E370000D1E685 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + A12016081E7E370000D1E685 /* DateTextField */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + A12016071E7E370000D1E685 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A1B755051EBDAFEE00472852 /* Assets.xcassets in Resources */, + A1B755071EBDAFEE00472852 /* Main.storyboard in Resources */, + A1B755061EBDAFEE00472852 /* LaunchScreen.storyboard in Resources */, + A1B755081EBDAFEE00472852 /* Info.plist in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + A12016051E7E370000D1E685 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A1B755091EBDAFEE00472852 /* ViewController.swift in Sources */, + A1B754F91EBDAFE200472852 /* DateTextField.swift in Sources */, + A1B755041EBDAFEE00472852 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + A1B754FD1EBDAFEE00472852 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + A1B754FE1EBDAFEE00472852 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; + A1B754FF1EBDAFEE00472852 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + A1B755001EBDAFEE00472852 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + A12016191E7E370100D1E685 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 10.2; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + A120161A1E7E370100D1E685 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 10.2; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + TARGETED_DEVICE_FAMILY = "1,2"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + A120161C1E7E370100D1E685 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = RCFBK97QU7; + INFOPLIST_FILE = "$(SRCROOT)/Demo/Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.pearpi.DateTextField; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Debug; + }; + A120161D1E7E370100D1E685 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = RCFBK97QU7; + INFOPLIST_FILE = "$(SRCROOT)/Demo/Info.plist"; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.pearpi.DateTextField; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + A12016041E7E370000D1E685 /* Build configuration list for PBXProject "DateTextField" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A12016191E7E370100D1E685 /* Debug */, + A120161A1E7E370100D1E685 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + A120161B1E7E370100D1E685 /* Build configuration list for PBXNativeTarget "DateTextField" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A120161C1E7E370100D1E685 /* Debug */, + A120161D1E7E370100D1E685 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = A12016011E7E370000D1E685 /* Project object */; +} diff --git a/DateTextField.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/DateTextField.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..83b9891 --- /dev/null +++ b/DateTextField.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/DateTextField/DateTextField.swift b/DateTextField/DateTextField.swift new file mode 100644 index 0000000..96c4b1d --- /dev/null +++ b/DateTextField/DateTextField.swift @@ -0,0 +1,179 @@ +// +// DateTextField.swift +// DateTextField +// +// Created by Beau Nouvelle on 19/3/17. +// Copyright © 2017 Beau Nouvelle. All rights reserved. +// + +import UIKit +import Foundation + +protocol DateTextFieldDelegate: class { + func dateDidChange(dateTextField: DateTextField) +} + +class DateTextField: UITextField { + + enum format: String { + case monthYear = "MM'$'yyyy" + case dayMonthYear = "dd'*'MM'$'yyyy" + case monthDayYear = "MM'$'dd'*'yyyy" + } + + // MARK: - Properties + private let digitOnlyRegex = try! NSRegularExpression(pattern: "[^0-9]+", options: NSRegularExpression.Options(rawValue: 0)) + private let dateFormatter = DateFormatter() + + var dateFormat = format.dayMonthYear + var separator: String = " / " + weak var customDelegate: DateTextFieldDelegate? + + /// Parses the `text` property into a `Date` and returns if successful. + var date: Date? { + get { + let format = dateFormat.rawValue.replacingOccurrences(of: "$", with: separator).replacingOccurrences(of: "*", with: separator) + dateFormatter.dateFormat = format + return dateFormatter.date(from: text ?? "") + } + set { + if newValue != nil { + let format = dateFormat.rawValue.replacingOccurrences(of: "$", with: separator).replacingOccurrences(of: "*", with: separator) + dateFormatter.dateFormat = format + text = dateFormatter.string(from: newValue!) + } else { + text = nil + } + } + } + + // MARK: - Lifecycle + override init(frame: CGRect) { + super.init(frame: frame) + setup() + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + setup() + } + + private func setup() { + super.delegate = self + keyboardType = .numberPad + autocorrectionType = .no + } + + func numberOnlyString(with string: String) -> String { + return digitOnlyRegex.stringByReplacingMatches(in: string, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, string.characters.count), withTemplate: "") + } + +} + +// MARK: - UITextFieldDelegate +extension DateTextField: UITextFieldDelegate { + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + + if string.characters.count == 0 { + customDelegate?.dateDidChange(dateTextField: self) + return true + } + + guard let swiftRange = textField.text?.getRange(from: range) else { return false } + guard let replacedString = textField.text?.replacingCharacters(in: swiftRange, with: string) else { return false } + + // Because you never know what people will paste in here, and some emoji have numbers present. + let emojiFreeString = replacedString.stringByRemovingEmoji() + let numbersOnly = numberOnlyString(with: emojiFreeString) + + switch dateFormat { + case .monthYear: + guard numbersOnly.characters.count <= 6 else { return false } + let splitString = split(string: numbersOnly, format: [2,4]) + textField.text = final(day: "", month: splitString.count > 0 ? splitString[0] : "", year: splitString.count > 1 ? splitString[1] : "") + case .dayMonthYear: + guard numbersOnly.characters.count <= 8 else { return false } + let splitString = split(string: numbersOnly, format: [2,2,4]) + textField.text = final(day: splitString.count > 0 ? splitString[0] : "", month: splitString.count > 1 ? splitString[1] : "", year: splitString.count > 2 ? splitString[2] : "") + case .monthDayYear: + guard numbersOnly.characters.count <= 8 else { return false } + let splitString = split(string: numbersOnly, format: [2,2,4]) + textField.text = final(day: splitString.count > 1 ? splitString[1] : "", month: splitString.count > 0 ? splitString[0] : "", year: splitString.count > 2 ? splitString[2] : "") + } + customDelegate?.dateDidChange(dateTextField: self) + return false + } + + func split(string: String, format: [Int]) -> [String] { + + var mutableString = string + var splitString = [String]() + + for item in format { + if mutableString.characters.count == 0 { + break + } + if mutableString.characters.count >= item { + let index = string.index(mutableString.startIndex, offsetBy: item) + splitString.append(mutableString.substring(to: index)) + mutableString.removeSubrange(Range(uncheckedBounds: (mutableString.startIndex, index))) + } else { + splitString.append(mutableString) + mutableString.removeSubrange(Range(uncheckedBounds: (mutableString.startIndex, mutableString.endIndex))) + } + } + + return splitString + } + + func final(day: String, month: String, year: String) -> String { + + var dateString = dateFormat.rawValue + dateString = dateString.replacingOccurrences(of: "dd", with: day) + dateString = dateString.replacingOccurrences(of: "MM", with: month) + dateString = dateString.replacingOccurrences(of: "yyyy", with: year) + + if day.characters.count >= 2 { + dateString = dateString.replacingOccurrences(of: "*", with: separator) + } else { + dateString = dateString.replacingOccurrences(of: "*", with: "") + } + if month.characters.count >= 2 { + dateString = dateString.replacingOccurrences(of: "$", with: separator) + } else { + dateString = dateString.replacingOccurrences(of: "$", with: "") + } + + return dateString.replacingOccurrences(of: "'", with: "") + } + +} + +// MARK: - String Extension +extension String { + + fileprivate func getRange(from nsRange: NSRange) -> Range? { + guard + let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex), + let to16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location + nsRange.length, limitedBy: utf16.endIndex), + let from = from16.samePosition(in: self), + let to = to16.samePosition(in: self) + else { return nil } + return from ..< to + } + + fileprivate func stringByRemovingEmoji() -> String { + return String(self.characters.filter { !$0.isEmoji() }) + } + +} + +// MARK: - Character Extension +extension Character { + fileprivate func isEmoji() -> Bool { + return Character(UnicodeScalar(UInt32(0x1d000))!) <= self && self <= Character(UnicodeScalar(UInt32(0x1f77f))!) + || Character(UnicodeScalar(UInt32(0x2100))!) <= self && self <= Character(UnicodeScalar(UInt32(0x26ff))!) + } +} + diff --git a/Demo/.DS_Store b/Demo/.DS_Store new file mode 100644 index 0000000..943d824 Binary files /dev/null and b/Demo/.DS_Store differ diff --git a/Demo/AppDelegate.swift b/Demo/AppDelegate.swift new file mode 100644 index 0000000..55a848a --- /dev/null +++ b/Demo/AppDelegate.swift @@ -0,0 +1,46 @@ +// +// AppDelegate.swift +// DateTextField +// +// Created by Beau Nouvelle on 19/3/17. +// Copyright © 2017 Beau Nouvelle. All rights reserved. +// + +import UIKit + +@UIApplicationMain +class AppDelegate: UIResponder, UIApplicationDelegate { + + var window: UIWindow? + + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + return true + } + + func applicationWillResignActive(_ application: UIApplication) { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + } + + func applicationDidEnterBackground(_ application: UIApplication) { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + } + + func applicationWillEnterForeground(_ application: UIApplication) { + // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + } + + func applicationDidBecomeActive(_ application: UIApplication) { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + } + + func applicationWillTerminate(_ application: UIApplication) { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. + } + + +} + diff --git a/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json b/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..1d060ed --- /dev/null +++ b/Demo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,93 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "76x76", + "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Demo/Base.lproj/LaunchScreen.storyboard b/Demo/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..fdf3f97 --- /dev/null +++ b/Demo/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Demo/Base.lproj/Main.storyboard b/Demo/Base.lproj/Main.storyboard new file mode 100644 index 0000000..32dc40c --- /dev/null +++ b/Demo/Base.lproj/Main.storyboard @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Demo/Info.plist b/Demo/Info.plist new file mode 100644 index 0000000..d052473 --- /dev/null +++ b/Demo/Info.plist @@ -0,0 +1,45 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/Demo/ViewController.swift b/Demo/ViewController.swift new file mode 100644 index 0000000..7261266 --- /dev/null +++ b/Demo/ViewController.swift @@ -0,0 +1,53 @@ +// +// ViewController.swift +// DateTextField +// +// Created by Beau Nouvelle on 19/3/17. +// Copyright © 2017 Beau Nouvelle. All rights reserved. +// + +import UIKit + +class ViewController: UIViewController { + + @IBOutlet weak var dateTextField: DateTextField! + @IBOutlet weak var displayDate: UILabel! + @IBOutlet weak var formatTypeSegmentedControl: UISegmentedControl! + + override func viewDidLoad() { + super.viewDidLoad() + dateTextField.becomeFirstResponder() + dateTextField.dateFormat = .monthYear + dateTextField.separator = "-" + } + + @IBAction func showDate(_ sender: Any) { + let formatter = DateFormatter() + formatter.dateStyle = .full + if dateTextField.date != nil { + displayDate.text = formatter.string(from: dateTextField.date!) + } + } + + @IBAction func dateFormatChanged(_ sender: UISegmentedControl) { + + switch sender.selectedSegmentIndex { + case 0: + dateTextField.placeholder = "MM/YYYY" + dateTextField.text = "" + dateTextField.dateFormat = .monthYear + case 1: + dateTextField.placeholder = "DD/MM/YYYY" + dateTextField.text = "" + dateTextField.dateFormat = .dayMonthYear + case 2: + dateTextField.placeholder = "MM/DD/YYYY" + dateTextField.text = "" + dateTextField.dateFormat = .monthDayYear + default: + preconditionFailure("Selected index not handled") + } + + } +} +