├── .gitignore ├── .swift-version ├── KSNumericTextField.podspec ├── KSNumericTextField ├── KSNumericTextField.xcodeproj │ ├── project.pbxproj │ └── xcshareddata │ │ └── xcschemes │ │ └── KSNumericTextField.xcscheme ├── KSNumericTextField │ ├── Info.plist │ ├── KSNumericTextField.h │ └── Sources │ │ └── KSNumericTextField.swift └── KSNumericTextFieldTests │ ├── Info.plist │ └── KSNumericTextFieldTests.swift ├── KSNumericTextFieldExample.xcworkspace └── contents.xcworkspacedata ├── KSNumericTextFieldExample ├── KSNumericTextFieldExample.xcodeproj │ └── project.pbxproj └── KSNumericTextFieldExample │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ └── ViewController.swift ├── LICENSE ├── README-Assets ├── AttributeInspector.png └── demo.gif └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | 42 | # CocoaPods 43 | # 44 | # We recommend against adding the Pods directory to your .gitignore. However 45 | # you should judge for yourself, the pros and cons are mentioned at: 46 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 47 | # 48 | # Pods/ 49 | 50 | # Carthage 51 | # 52 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 53 | # Carthage/Checkouts 54 | 55 | Carthage/Build 56 | 57 | # fastlane 58 | # 59 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 60 | # screenshots whenever they are needed. 61 | # For more information about the recommended setup visit: 62 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 63 | 64 | fastlane/report.xml 65 | fastlane/Preview.html 66 | fastlane/screenshots 67 | fastlane/test_output 68 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 4.2 2 | -------------------------------------------------------------------------------- /KSNumericTextField.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'KSNumericTextField' 3 | s.version = '1.0.0' 4 | s.summary = 'text field that only accept numeric value.' 5 | 6 | s.description = 'KSNumericTextField is a simple to use text field that only accept numeric value. You can also specify the maximum number of integer digits and faction digits using storyboard or code.' 7 | 8 | s.homepage = 'https://github.com/LeeKahSeng/KSNumericTextField' 9 | s.license = { :type => 'MIT', :file => 'LICENSE' } 10 | s.author = { 'Lee Kah Seng' => 'kahseng.lee123@gmail.com' } 11 | s.source = { 12 | :git => 'https://github.com/LeeKahSeng/KSNumericTextField.git', 13 | :tag => s.version.to_s, 14 | :branch => 'master' 15 | } 16 | 17 | s.swift_version = '4.2' 18 | s.ios.deployment_target = '11.2' 19 | s.source_files = 'KSNumericTextField/KSNumericTextField/Sources/*.swift' 20 | 21 | end -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextField.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F988AF04205D4F2D00C93F03 /* KSNumericTextField.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F988AEFA205D4F2D00C93F03 /* KSNumericTextField.framework */; }; 11 | F988AF09205D4F2D00C93F03 /* KSNumericTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F988AF08205D4F2D00C93F03 /* KSNumericTextFieldTests.swift */; }; 12 | F988AF0B205D4F2D00C93F03 /* KSNumericTextField.h in Headers */ = {isa = PBXBuildFile; fileRef = F988AEFD205D4F2D00C93F03 /* KSNumericTextField.h */; settings = {ATTRIBUTES = (Public, ); }; }; 13 | F9C6E97F20610EEA0092B549 /* KSNumericTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9C6E97E20610EEA0092B549 /* KSNumericTextField.swift */; }; 14 | /* End PBXBuildFile section */ 15 | 16 | /* Begin PBXContainerItemProxy section */ 17 | F988AF05205D4F2D00C93F03 /* PBXContainerItemProxy */ = { 18 | isa = PBXContainerItemProxy; 19 | containerPortal = F988AEF1205D4F2D00C93F03 /* Project object */; 20 | proxyType = 1; 21 | remoteGlobalIDString = F988AEF9205D4F2D00C93F03; 22 | remoteInfo = KSNumericTextField; 23 | }; 24 | /* End PBXContainerItemProxy section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | F988AEFA205D4F2D00C93F03 /* KSNumericTextField.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KSNumericTextField.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | F988AEFD205D4F2D00C93F03 /* KSNumericTextField.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KSNumericTextField.h; sourceTree = ""; }; 29 | F988AEFE205D4F2D00C93F03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | F988AF03205D4F2D00C93F03 /* KSNumericTextFieldTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KSNumericTextFieldTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | F988AF08205D4F2D00C93F03 /* KSNumericTextFieldTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KSNumericTextFieldTests.swift; sourceTree = ""; }; 32 | F988AF0A205D4F2D00C93F03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | F9C6E97E20610EEA0092B549 /* KSNumericTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KSNumericTextField.swift; sourceTree = ""; }; 34 | /* End PBXFileReference section */ 35 | 36 | /* Begin PBXFrameworksBuildPhase section */ 37 | F988AEF6205D4F2D00C93F03 /* Frameworks */ = { 38 | isa = PBXFrameworksBuildPhase; 39 | buildActionMask = 2147483647; 40 | files = ( 41 | ); 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | F988AF00205D4F2D00C93F03 /* Frameworks */ = { 45 | isa = PBXFrameworksBuildPhase; 46 | buildActionMask = 2147483647; 47 | files = ( 48 | F988AF04205D4F2D00C93F03 /* KSNumericTextField.framework in Frameworks */, 49 | ); 50 | runOnlyForDeploymentPostprocessing = 0; 51 | }; 52 | /* End PBXFrameworksBuildPhase section */ 53 | 54 | /* Begin PBXGroup section */ 55 | F988AEF0205D4F2D00C93F03 = { 56 | isa = PBXGroup; 57 | children = ( 58 | F988AEFC205D4F2D00C93F03 /* KSNumericTextField */, 59 | F988AF07205D4F2D00C93F03 /* KSNumericTextFieldTests */, 60 | F988AEFB205D4F2D00C93F03 /* Products */, 61 | ); 62 | sourceTree = ""; 63 | }; 64 | F988AEFB205D4F2D00C93F03 /* Products */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | F988AEFA205D4F2D00C93F03 /* KSNumericTextField.framework */, 68 | F988AF03205D4F2D00C93F03 /* KSNumericTextFieldTests.xctest */, 69 | ); 70 | name = Products; 71 | sourceTree = ""; 72 | }; 73 | F988AEFC205D4F2D00C93F03 /* KSNumericTextField */ = { 74 | isa = PBXGroup; 75 | children = ( 76 | F9C6E97D20610ED50092B549 /* Sources */, 77 | F988AEFD205D4F2D00C93F03 /* KSNumericTextField.h */, 78 | F988AEFE205D4F2D00C93F03 /* Info.plist */, 79 | ); 80 | path = KSNumericTextField; 81 | sourceTree = ""; 82 | }; 83 | F988AF07205D4F2D00C93F03 /* KSNumericTextFieldTests */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | F988AF08205D4F2D00C93F03 /* KSNumericTextFieldTests.swift */, 87 | F988AF0A205D4F2D00C93F03 /* Info.plist */, 88 | ); 89 | path = KSNumericTextFieldTests; 90 | sourceTree = ""; 91 | }; 92 | F9C6E97D20610ED50092B549 /* Sources */ = { 93 | isa = PBXGroup; 94 | children = ( 95 | F9C6E97E20610EEA0092B549 /* KSNumericTextField.swift */, 96 | ); 97 | path = Sources; 98 | sourceTree = ""; 99 | }; 100 | /* End PBXGroup section */ 101 | 102 | /* Begin PBXHeadersBuildPhase section */ 103 | F988AEF7205D4F2D00C93F03 /* Headers */ = { 104 | isa = PBXHeadersBuildPhase; 105 | buildActionMask = 2147483647; 106 | files = ( 107 | F988AF0B205D4F2D00C93F03 /* KSNumericTextField.h in Headers */, 108 | ); 109 | runOnlyForDeploymentPostprocessing = 0; 110 | }; 111 | /* End PBXHeadersBuildPhase section */ 112 | 113 | /* Begin PBXNativeTarget section */ 114 | F988AEF9205D4F2D00C93F03 /* KSNumericTextField */ = { 115 | isa = PBXNativeTarget; 116 | buildConfigurationList = F988AF0E205D4F2D00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextField" */; 117 | buildPhases = ( 118 | F988AEF5205D4F2D00C93F03 /* Sources */, 119 | F988AEF6205D4F2D00C93F03 /* Frameworks */, 120 | F988AEF7205D4F2D00C93F03 /* Headers */, 121 | F988AEF8205D4F2D00C93F03 /* Resources */, 122 | ); 123 | buildRules = ( 124 | ); 125 | dependencies = ( 126 | ); 127 | name = KSNumericTextField; 128 | productName = KSNumericTextField; 129 | productReference = F988AEFA205D4F2D00C93F03 /* KSNumericTextField.framework */; 130 | productType = "com.apple.product-type.framework"; 131 | }; 132 | F988AF02205D4F2D00C93F03 /* KSNumericTextFieldTests */ = { 133 | isa = PBXNativeTarget; 134 | buildConfigurationList = F988AF11205D4F2D00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextFieldTests" */; 135 | buildPhases = ( 136 | F988AEFF205D4F2D00C93F03 /* Sources */, 137 | F988AF00205D4F2D00C93F03 /* Frameworks */, 138 | F988AF01205D4F2D00C93F03 /* Resources */, 139 | ); 140 | buildRules = ( 141 | ); 142 | dependencies = ( 143 | F988AF06205D4F2D00C93F03 /* PBXTargetDependency */, 144 | ); 145 | name = KSNumericTextFieldTests; 146 | productName = KSNumericTextFieldTests; 147 | productReference = F988AF03205D4F2D00C93F03 /* KSNumericTextFieldTests.xctest */; 148 | productType = "com.apple.product-type.bundle.unit-test"; 149 | }; 150 | /* End PBXNativeTarget section */ 151 | 152 | /* Begin PBXProject section */ 153 | F988AEF1205D4F2D00C93F03 /* Project object */ = { 154 | isa = PBXProject; 155 | attributes = { 156 | LastSwiftUpdateCheck = 0920; 157 | LastUpgradeCheck = 1010; 158 | ORGANIZATIONNAME = "Lee Kah Seng"; 159 | TargetAttributes = { 160 | F988AEF9205D4F2D00C93F03 = { 161 | CreatedOnToolsVersion = 9.2; 162 | LastSwiftMigration = 1010; 163 | ProvisioningStyle = Automatic; 164 | }; 165 | F988AF02205D4F2D00C93F03 = { 166 | CreatedOnToolsVersion = 9.2; 167 | ProvisioningStyle = Automatic; 168 | }; 169 | }; 170 | }; 171 | buildConfigurationList = F988AEF4205D4F2D00C93F03 /* Build configuration list for PBXProject "KSNumericTextField" */; 172 | compatibilityVersion = "Xcode 8.0"; 173 | developmentRegion = en; 174 | hasScannedForEncodings = 0; 175 | knownRegions = ( 176 | en, 177 | ); 178 | mainGroup = F988AEF0205D4F2D00C93F03; 179 | productRefGroup = F988AEFB205D4F2D00C93F03 /* Products */; 180 | projectDirPath = ""; 181 | projectRoot = ""; 182 | targets = ( 183 | F988AEF9205D4F2D00C93F03 /* KSNumericTextField */, 184 | F988AF02205D4F2D00C93F03 /* KSNumericTextFieldTests */, 185 | ); 186 | }; 187 | /* End PBXProject section */ 188 | 189 | /* Begin PBXResourcesBuildPhase section */ 190 | F988AEF8205D4F2D00C93F03 /* Resources */ = { 191 | isa = PBXResourcesBuildPhase; 192 | buildActionMask = 2147483647; 193 | files = ( 194 | ); 195 | runOnlyForDeploymentPostprocessing = 0; 196 | }; 197 | F988AF01205D4F2D00C93F03 /* Resources */ = { 198 | isa = PBXResourcesBuildPhase; 199 | buildActionMask = 2147483647; 200 | files = ( 201 | ); 202 | runOnlyForDeploymentPostprocessing = 0; 203 | }; 204 | /* End PBXResourcesBuildPhase section */ 205 | 206 | /* Begin PBXSourcesBuildPhase section */ 207 | F988AEF5205D4F2D00C93F03 /* Sources */ = { 208 | isa = PBXSourcesBuildPhase; 209 | buildActionMask = 2147483647; 210 | files = ( 211 | F9C6E97F20610EEA0092B549 /* KSNumericTextField.swift in Sources */, 212 | ); 213 | runOnlyForDeploymentPostprocessing = 0; 214 | }; 215 | F988AEFF205D4F2D00C93F03 /* Sources */ = { 216 | isa = PBXSourcesBuildPhase; 217 | buildActionMask = 2147483647; 218 | files = ( 219 | F988AF09205D4F2D00C93F03 /* KSNumericTextFieldTests.swift in Sources */, 220 | ); 221 | runOnlyForDeploymentPostprocessing = 0; 222 | }; 223 | /* End PBXSourcesBuildPhase section */ 224 | 225 | /* Begin PBXTargetDependency section */ 226 | F988AF06205D4F2D00C93F03 /* PBXTargetDependency */ = { 227 | isa = PBXTargetDependency; 228 | target = F988AEF9205D4F2D00C93F03 /* KSNumericTextField */; 229 | targetProxy = F988AF05205D4F2D00C93F03 /* PBXContainerItemProxy */; 230 | }; 231 | /* End PBXTargetDependency section */ 232 | 233 | /* Begin XCBuildConfiguration section */ 234 | F988AF0C205D4F2D00C93F03 /* Debug */ = { 235 | isa = XCBuildConfiguration; 236 | buildSettings = { 237 | ALWAYS_SEARCH_USER_PATHS = NO; 238 | CLANG_ANALYZER_NONNULL = YES; 239 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 240 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 241 | CLANG_CXX_LIBRARY = "libc++"; 242 | CLANG_ENABLE_MODULES = YES; 243 | CLANG_ENABLE_OBJC_ARC = YES; 244 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 245 | CLANG_WARN_BOOL_CONVERSION = YES; 246 | CLANG_WARN_COMMA = YES; 247 | CLANG_WARN_CONSTANT_CONVERSION = YES; 248 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 249 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 250 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 251 | CLANG_WARN_EMPTY_BODY = YES; 252 | CLANG_WARN_ENUM_CONVERSION = YES; 253 | CLANG_WARN_INFINITE_RECURSION = YES; 254 | CLANG_WARN_INT_CONVERSION = YES; 255 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 257 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 258 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 259 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 260 | CLANG_WARN_STRICT_PROTOTYPES = YES; 261 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 262 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 263 | CLANG_WARN_UNREACHABLE_CODE = YES; 264 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 265 | CODE_SIGN_IDENTITY = "iPhone Developer"; 266 | COPY_PHASE_STRIP = NO; 267 | CURRENT_PROJECT_VERSION = 1; 268 | DEBUG_INFORMATION_FORMAT = dwarf; 269 | ENABLE_STRICT_OBJC_MSGSEND = YES; 270 | ENABLE_TESTABILITY = YES; 271 | GCC_C_LANGUAGE_STANDARD = gnu11; 272 | GCC_DYNAMIC_NO_PIC = NO; 273 | GCC_NO_COMMON_BLOCKS = YES; 274 | GCC_OPTIMIZATION_LEVEL = 0; 275 | GCC_PREPROCESSOR_DEFINITIONS = ( 276 | "DEBUG=1", 277 | "$(inherited)", 278 | ); 279 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 280 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 281 | GCC_WARN_UNDECLARED_SELECTOR = YES; 282 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 283 | GCC_WARN_UNUSED_FUNCTION = YES; 284 | GCC_WARN_UNUSED_VARIABLE = YES; 285 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 286 | MTL_ENABLE_DEBUG_INFO = YES; 287 | ONLY_ACTIVE_ARCH = YES; 288 | SDKROOT = iphoneos; 289 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 290 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 291 | VERSIONING_SYSTEM = "apple-generic"; 292 | VERSION_INFO_PREFIX = ""; 293 | }; 294 | name = Debug; 295 | }; 296 | F988AF0D205D4F2D00C93F03 /* Release */ = { 297 | isa = XCBuildConfiguration; 298 | buildSettings = { 299 | ALWAYS_SEARCH_USER_PATHS = NO; 300 | CLANG_ANALYZER_NONNULL = YES; 301 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 302 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 303 | CLANG_CXX_LIBRARY = "libc++"; 304 | CLANG_ENABLE_MODULES = YES; 305 | CLANG_ENABLE_OBJC_ARC = YES; 306 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 307 | CLANG_WARN_BOOL_CONVERSION = YES; 308 | CLANG_WARN_COMMA = YES; 309 | CLANG_WARN_CONSTANT_CONVERSION = YES; 310 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 311 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 312 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 313 | CLANG_WARN_EMPTY_BODY = YES; 314 | CLANG_WARN_ENUM_CONVERSION = YES; 315 | CLANG_WARN_INFINITE_RECURSION = YES; 316 | CLANG_WARN_INT_CONVERSION = YES; 317 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 318 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 319 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 320 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 321 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 322 | CLANG_WARN_STRICT_PROTOTYPES = YES; 323 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 324 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 325 | CLANG_WARN_UNREACHABLE_CODE = YES; 326 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 327 | CODE_SIGN_IDENTITY = "iPhone Developer"; 328 | COPY_PHASE_STRIP = NO; 329 | CURRENT_PROJECT_VERSION = 1; 330 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 331 | ENABLE_NS_ASSERTIONS = NO; 332 | ENABLE_STRICT_OBJC_MSGSEND = YES; 333 | GCC_C_LANGUAGE_STANDARD = gnu11; 334 | GCC_NO_COMMON_BLOCKS = YES; 335 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 336 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 337 | GCC_WARN_UNDECLARED_SELECTOR = YES; 338 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 339 | GCC_WARN_UNUSED_FUNCTION = YES; 340 | GCC_WARN_UNUSED_VARIABLE = YES; 341 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 342 | MTL_ENABLE_DEBUG_INFO = NO; 343 | SDKROOT = iphoneos; 344 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 345 | VALIDATE_PRODUCT = YES; 346 | VERSIONING_SYSTEM = "apple-generic"; 347 | VERSION_INFO_PREFIX = ""; 348 | }; 349 | name = Release; 350 | }; 351 | F988AF0F205D4F2D00C93F03 /* Debug */ = { 352 | isa = XCBuildConfiguration; 353 | buildSettings = { 354 | CLANG_ENABLE_MODULES = YES; 355 | CODE_SIGN_IDENTITY = ""; 356 | CODE_SIGN_STYLE = Automatic; 357 | DEFINES_MODULE = YES; 358 | DEVELOPMENT_TEAM = SM42V25455; 359 | DYLIB_COMPATIBILITY_VERSION = 1; 360 | DYLIB_CURRENT_VERSION = 1; 361 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 362 | INFOPLIST_FILE = KSNumericTextField/Info.plist; 363 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 364 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 365 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 366 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextField; 367 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 368 | SKIP_INSTALL = YES; 369 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 370 | SWIFT_VERSION = 4.2; 371 | TARGETED_DEVICE_FAMILY = "1,2"; 372 | }; 373 | name = Debug; 374 | }; 375 | F988AF10205D4F2D00C93F03 /* Release */ = { 376 | isa = XCBuildConfiguration; 377 | buildSettings = { 378 | CLANG_ENABLE_MODULES = YES; 379 | CODE_SIGN_IDENTITY = ""; 380 | CODE_SIGN_STYLE = Automatic; 381 | DEFINES_MODULE = YES; 382 | DEVELOPMENT_TEAM = SM42V25455; 383 | DYLIB_COMPATIBILITY_VERSION = 1; 384 | DYLIB_CURRENT_VERSION = 1; 385 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 386 | INFOPLIST_FILE = KSNumericTextField/Info.plist; 387 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 388 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 389 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 390 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextField; 391 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 392 | SKIP_INSTALL = YES; 393 | SWIFT_VERSION = 4.2; 394 | TARGETED_DEVICE_FAMILY = "1,2"; 395 | }; 396 | name = Release; 397 | }; 398 | F988AF12205D4F2D00C93F03 /* Debug */ = { 399 | isa = XCBuildConfiguration; 400 | buildSettings = { 401 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 402 | CODE_SIGN_STYLE = Automatic; 403 | DEVELOPMENT_TEAM = SM42V25455; 404 | INFOPLIST_FILE = KSNumericTextFieldTests/Info.plist; 405 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 406 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextFieldTests; 407 | PRODUCT_NAME = "$(TARGET_NAME)"; 408 | SWIFT_VERSION = 4.0; 409 | TARGETED_DEVICE_FAMILY = "1,2"; 410 | }; 411 | name = Debug; 412 | }; 413 | F988AF13205D4F2D00C93F03 /* Release */ = { 414 | isa = XCBuildConfiguration; 415 | buildSettings = { 416 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 417 | CODE_SIGN_STYLE = Automatic; 418 | DEVELOPMENT_TEAM = SM42V25455; 419 | INFOPLIST_FILE = KSNumericTextFieldTests/Info.plist; 420 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 421 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextFieldTests; 422 | PRODUCT_NAME = "$(TARGET_NAME)"; 423 | SWIFT_VERSION = 4.0; 424 | TARGETED_DEVICE_FAMILY = "1,2"; 425 | }; 426 | name = Release; 427 | }; 428 | /* End XCBuildConfiguration section */ 429 | 430 | /* Begin XCConfigurationList section */ 431 | F988AEF4205D4F2D00C93F03 /* Build configuration list for PBXProject "KSNumericTextField" */ = { 432 | isa = XCConfigurationList; 433 | buildConfigurations = ( 434 | F988AF0C205D4F2D00C93F03 /* Debug */, 435 | F988AF0D205D4F2D00C93F03 /* Release */, 436 | ); 437 | defaultConfigurationIsVisible = 0; 438 | defaultConfigurationName = Release; 439 | }; 440 | F988AF0E205D4F2D00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextField" */ = { 441 | isa = XCConfigurationList; 442 | buildConfigurations = ( 443 | F988AF0F205D4F2D00C93F03 /* Debug */, 444 | F988AF10205D4F2D00C93F03 /* Release */, 445 | ); 446 | defaultConfigurationIsVisible = 0; 447 | defaultConfigurationName = Release; 448 | }; 449 | F988AF11205D4F2D00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextFieldTests" */ = { 450 | isa = XCConfigurationList; 451 | buildConfigurations = ( 452 | F988AF12205D4F2D00C93F03 /* Debug */, 453 | F988AF13205D4F2D00C93F03 /* Release */, 454 | ); 455 | defaultConfigurationIsVisible = 0; 456 | defaultConfigurationName = Release; 457 | }; 458 | /* End XCConfigurationList section */ 459 | }; 460 | rootObject = F988AEF1205D4F2D00C93F03 /* Project object */; 461 | } 462 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextField.xcodeproj/xcshareddata/xcschemes/KSNumericTextField.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextField/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSPrincipalClass 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextField/KSNumericTextField.h: -------------------------------------------------------------------------------- 1 | // 2 | // KSNumericTextField.h 3 | // KSNumericTextField 4 | // 5 | // Created by Lee Kah Seng on 17/03/2018. 6 | // Copyright © 2018 Lee Kah Seng. All rights reserved. (https://github.com/LeeKahSeng/KSNumericTextField) 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | // 26 | 27 | #import 28 | 29 | //! Project version number for KSNumericTextField. 30 | FOUNDATION_EXPORT double KSNumericTextFieldVersionNumber; 31 | 32 | //! Project version string for KSNumericTextField. 33 | FOUNDATION_EXPORT const unsigned char KSNumericTextFieldVersionString[]; 34 | 35 | // In this header, you should import all the public headers of your framework using statements like #import 36 | 37 | 38 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextField/Sources/KSNumericTextField.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KSNumericTextField.swift 3 | // KS-Firebase-Playground 4 | // 5 | // Created by Lee Kah Seng on 14/03/2017. 6 | // Copyright © 2018 Lee Kah Seng. All rights reserved. (https://github.com/LeeKahSeng/KSNumericTextField) 7 | // 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy 9 | // of this software and associated documentation files (the "Software"), to deal 10 | // in the Software without restriction, including without limitation the rights 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | // copies of the Software, and to permit persons to whom the Software is 13 | // furnished to do so, subject to the following conditions: 14 | // 15 | // The above copyright notice and this permission notice shall be included in all 16 | // copies or substantial portions of the Software. 17 | // 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | // SOFTWARE. 25 | // 26 | 27 | 28 | import UIKit 29 | 30 | @objc public protocol KSNumericTextFieldDelegate { 31 | 32 | @available(iOS 2.0, *) 33 | @objc optional func textFieldShouldBeginEditing(_ textField: KSNumericTextField) -> Bool 34 | 35 | @available(iOS 2.0, *) 36 | @objc optional func textFieldDidBeginEditing(_ textField: KSNumericTextField) 37 | 38 | @available(iOS 2.0, *) 39 | @objc optional func textFieldShouldEndEditing(_ textField: KSNumericTextField) -> Bool 40 | 41 | @available(iOS 2.0, *) 42 | @objc optional func textFieldDidEndEditing(_ textField: KSNumericTextField) 43 | 44 | @available(iOS 2.0, *) 45 | @objc optional func textField(_ textField: KSNumericTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 46 | 47 | @available(iOS 2.0, *) 48 | @objc optional func textFieldShouldClear(_ textField: KSNumericTextField) -> Bool 49 | 50 | @available(iOS 2.0, *) 51 | @objc optional func textFieldShouldReturn(_ textField: KSNumericTextField) -> Bool 52 | 53 | @available(iOS 10.0, *) 54 | @objc optional func textFieldDidEndEditing(_ textField: KSNumericTextField, reason: UITextField.DidEndEditingReason) 55 | } 56 | 57 | public class KSNumericTextField: UITextField { 58 | 59 | public weak var numericTextFieldDelegate: KSNumericTextFieldDelegate? 60 | 61 | @IBInspectable public var maxIntegerDigit: Int = 3 { 62 | didSet { 63 | if maxIntegerDigit < 1 { 64 | fatalError("maxIntegerDigit must be larger than 0") 65 | 66 | } 67 | } 68 | } 69 | @IBInspectable public var maxFractionDigit: Int = 2 70 | 71 | private let separator = Locale.current.decimalSeparator! 72 | 73 | override public var text: String? { 74 | get { 75 | return super.text 76 | } 77 | 78 | set { 79 | if isValid(newValue, maxIntegerDigit: maxIntegerDigit, maxFractionDigit: maxFractionDigit, decimalSeparator: separator) { 80 | 81 | // String format is valid 82 | super.text = newValue 83 | } 84 | } 85 | } 86 | 87 | required public init(coder aDecoder: NSCoder) { 88 | super.init(coder: aDecoder)! 89 | 90 | commonInit() 91 | } 92 | 93 | override init(frame: CGRect) { 94 | super.init(frame: frame) 95 | 96 | commonInit() 97 | } 98 | 99 | convenience init(withMaxIntegerDigit integerDigit: Int, maxFractionDigit fractionDigit: Int) { 100 | self.init(frame: CGRect.zero) 101 | 102 | maxIntegerDigit = integerDigit 103 | maxFractionDigit = fractionDigit 104 | } 105 | 106 | override public func didMoveToWindow() { 107 | super.didMoveToWindow() 108 | 109 | let isTextValid = isValid(super.text, maxIntegerDigit: maxIntegerDigit, maxFractionDigit: maxFractionDigit, decimalSeparator: separator) 110 | if !isTextValid { 111 | // Remove string in textField when invalid string was set in interface builder 112 | super.text = "" 113 | } 114 | } 115 | 116 | 117 | // Note: Override delegate to make sure super.delegate is always self 118 | override public var delegate: UITextFieldDelegate? { 119 | get { 120 | return super.delegate 121 | } 122 | 123 | set { 124 | if let d = newValue, d.isKind(of: KSNumericTextField.self) { 125 | super.delegate = newValue 126 | } 127 | } 128 | } 129 | } 130 | 131 | // MARK:- Public functions 132 | extension KSNumericTextField { 133 | 134 | /// Set string to textField and return boolean to indicate the string is it in valid format 135 | /// 136 | /// - Parameter value: string to set to textField 137 | /// - Returns: true: string is valid, false: string is invalid 138 | public func setText(_ value: String?) -> Bool { 139 | 140 | text = value 141 | return text == value 142 | } 143 | } 144 | 145 | // MARK:- Private functions 146 | extension KSNumericTextField { 147 | 148 | private func commonInit() { 149 | 150 | keyboardType = .decimalPad 151 | delegate = self 152 | } 153 | 154 | /// Check is the string a valid numeric value 155 | /// 156 | /// - Parameters: 157 | /// - string: string to validate 158 | /// - maxIntegerDigit: maximum whole number digit allow 159 | /// - maxFractionDigit: maximum decimal digit allow 160 | /// - separator: decimal separator 161 | /// - Returns: is the string have valid numeric value 162 | private func isValid(_ string: String?, maxIntegerDigit integerDigit: Int, maxFractionDigit fractionDigit: Int, decimalSeparator separator: String) -> Bool { 163 | 164 | guard let value = string else { 165 | return false 166 | } 167 | 168 | // Default regex pattern 169 | var pattern = "^\\d{0,\(integerDigit)}(\\\(separator)\\d{0,\(fractionDigit)})?$" 170 | 171 | if maxFractionDigit == 0 { 172 | // Use another regex pattern when fractionDigit == 0 173 | pattern = "^[0-9]{0,\(maxIntegerDigit)}$" 174 | } 175 | 176 | do { 177 | let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive) 178 | let result = regex.matches(in: value, options: [], range: NSMakeRange(0, value.count)) 179 | 180 | return (result.count > 0) 181 | 182 | } catch { 183 | return false 184 | } 185 | } 186 | 187 | /// Use to process the string after text field finish editing. Mainly to handle condition where separator at start / end of string 188 | /// 189 | /// - Parameter textFieldString: text value of textField 190 | /// - Returns: the processed string 191 | private func processStringAfterEndEditing(_ textFieldString: String?) -> String? { 192 | 193 | guard let str = textFieldString else { 194 | return nil 195 | } 196 | 197 | let endString = str as NSString 198 | let range = endString.range(of: separator) 199 | 200 | // Check is decimal separator at start or end 201 | if range.location == 0 { 202 | 203 | // Decimal separator at start, fill up 0 204 | return "0\(endString)" 205 | 206 | } else if range.location == endString.length - 1 { 207 | 208 | // Decimal separator at end, remove the separator 209 | return endString.substring(to: endString.length - 1) 210 | } 211 | 212 | return endString as String 213 | } 214 | } 215 | 216 | // MARK:- UITextFieldDelegate 217 | extension KSNumericTextField: UITextFieldDelegate { 218 | 219 | public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 220 | 221 | if let result = numericTextFieldDelegate?.textField?(self, shouldChangeCharactersIn: range, replacementString: string), result == false { 222 | 223 | // Do not change characters in range when user implemented delegate & return false as result 224 | return false 225 | 226 | } else { 227 | 228 | // Validate input string when user type / cut & paste 229 | 230 | // Remove leading & trailing white space 231 | let enteredText = string.trimmingCharacters(in: .whitespacesAndNewlines) 232 | 233 | let currentText = textField.text ?? "" 234 | let newText = (currentText as NSString).replacingCharacters(in: range, with: enteredText) 235 | 236 | // Set newText to text -> text set observer will check is newText format valid 237 | let isNewTextValid = setText(newText) 238 | if isNewTextValid { 239 | 240 | // newText have valid format, thus set cursor to correct position 241 | let cursorPosition = range.location 242 | if let newPosition = textField.position(from: textField.beginningOfDocument, offset: cursorPosition + enteredText.count) { 243 | 244 | textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition) 245 | } 246 | } 247 | 248 | return false 249 | } 250 | } 251 | 252 | public func textFieldDidEndEditing(_ textField: UITextField) { 253 | 254 | text = processStringAfterEndEditing(textField.text) 255 | 256 | numericTextFieldDelegate?.textFieldDidEndEditing?(self) 257 | } 258 | 259 | public func textFieldShouldClear(_ textField: UITextField) -> Bool { 260 | 261 | // Handle clear button tapped 262 | if let result = numericTextFieldDelegate?.textFieldShouldClear?(self), result == false { 263 | 264 | // Do not clear text field when user implemented delegate & return false as result 265 | return false 266 | } else { 267 | 268 | text = "" 269 | return true 270 | } 271 | } 272 | 273 | public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 274 | 275 | if let result = numericTextFieldDelegate?.textFieldShouldBeginEditing?(self) { 276 | return result 277 | } 278 | 279 | return true 280 | } 281 | 282 | public func textFieldDidBeginEditing(_ textField: UITextField) { 283 | numericTextFieldDelegate?.textFieldDidBeginEditing?(self) 284 | } 285 | 286 | public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { 287 | 288 | if let result = numericTextFieldDelegate?.textFieldShouldEndEditing?(self) { 289 | return result 290 | } 291 | 292 | return true 293 | } 294 | 295 | public func textFieldShouldReturn(_ textField: UITextField) -> Bool { 296 | if let result = numericTextFieldDelegate?.textFieldShouldReturn?(self) { 297 | return result 298 | } 299 | 300 | return true 301 | } 302 | 303 | @available(iOS 10.0, *) 304 | public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) { 305 | 306 | text = processStringAfterEndEditing(textField.text) 307 | 308 | numericTextFieldDelegate?.textFieldDidEndEditing?(self, reason: reason) 309 | } 310 | } 311 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextFieldTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /KSNumericTextField/KSNumericTextFieldTests/KSNumericTextFieldTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KSNumericTextFieldTests.swift 3 | // KSNumericTextFieldTests 4 | // 5 | // Created by Lee Kah Seng on 17/03/2018. 6 | // Copyright © 2018 Lee Kah Seng. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import KSNumericTextField 11 | 12 | class KSNumericTextFieldTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | 25 | func testValidInput() { 26 | 27 | let integerDigit = 5 28 | let fractionDigit = 3 29 | 30 | let textField = KSNumericTextField(withMaxIntegerDigit: integerDigit, maxFractionDigit: fractionDigit) 31 | 32 | // 1.3 33 | var inputText = "1.3" 34 | var expectedText = inputText 35 | 36 | var valid = textField.setText(inputText) 37 | var resultText = textField.text! 38 | 39 | XCTAssertTrue(valid) 40 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 41 | 42 | 43 | // 123 44 | inputText = "123" 45 | expectedText = inputText 46 | valid = textField.setText(inputText) 47 | resultText = textField.text! 48 | 49 | XCTAssertTrue(valid) 50 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 51 | 52 | 53 | // 0.234 54 | inputText = "0.234" 55 | expectedText = inputText 56 | valid = textField.setText(inputText) 57 | resultText = textField.text! 58 | 59 | XCTAssertTrue(valid) 60 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 61 | 62 | 63 | // 12345.123 64 | inputText = "12345.123" 65 | expectedText = inputText 66 | valid = textField.setText(inputText) 67 | resultText = textField.text! 68 | 69 | XCTAssertTrue(valid) 70 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 71 | } 72 | 73 | 74 | func testInValidInput() { 75 | 76 | let integerDigit = 3 77 | let fractionDigit = 2 78 | 79 | let textField = KSNumericTextField(withMaxIntegerDigit: integerDigit, maxFractionDigit: fractionDigit) 80 | 81 | // ABC 82 | var inputText = "ABC" 83 | var expectedText = "" 84 | 85 | var valid = textField.setText(inputText) 86 | var resultText = textField.text! 87 | 88 | XCTAssertFalse(valid) 89 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 90 | 91 | 92 | // 123 93 | inputText = "1234" 94 | expectedText = "" 95 | valid = textField.setText(inputText) 96 | resultText = textField.text! 97 | 98 | XCTAssertFalse(valid) 99 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 100 | 101 | 102 | // 0.234 103 | inputText = "0.999" 104 | expectedText = "" 105 | valid = textField.setText(inputText) 106 | resultText = textField.text! 107 | 108 | XCTAssertFalse(valid) 109 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 110 | 111 | 112 | // 12345.123 113 | inputText = "9123.444" 114 | expectedText = "" 115 | valid = textField.setText(inputText) 116 | resultText = textField.text! 117 | 118 | XCTAssertFalse(valid) 119 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 120 | } 121 | 122 | 123 | func testStartWithDecimalSeparator() { 124 | 125 | let integerDigit = 2 126 | let fractionDigit = 3 127 | 128 | let textField = KSNumericTextField(withMaxIntegerDigit: integerDigit, maxFractionDigit: fractionDigit) 129 | 130 | let inputText = ".23" 131 | let expectedText = "0.23" 132 | 133 | // Set inputText to textField & trigger end editing 134 | let valid = textField.setText(inputText) 135 | textField.textFieldDidEndEditing(textField) 136 | 137 | let resultText = textField.text! 138 | 139 | XCTAssertTrue(valid) 140 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 141 | } 142 | 143 | 144 | func testEndWithDecimalSeparator() { 145 | 146 | let integerDigit = 2 147 | let fractionDigit = 3 148 | 149 | let textField = KSNumericTextField(withMaxIntegerDigit: integerDigit, maxFractionDigit: fractionDigit) 150 | 151 | let inputText = "34." 152 | let expectedText = "34" 153 | 154 | // Set inputText to textField & trigger end editing 155 | let valid = textField.setText(inputText) 156 | textField.textFieldDidEndEditing(textField) 157 | 158 | let resultText = textField.text! 159 | 160 | XCTAssertTrue(valid) 161 | XCTAssertTrue(resultText == expectedText, "Expected \(expectedText), but get \(resultText)") 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F988AF21205D4F8B00C93F03 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F988AF20205D4F8B00C93F03 /* AppDelegate.swift */; }; 11 | F988AF23205D4F8B00C93F03 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F988AF22205D4F8B00C93F03 /* ViewController.swift */; }; 12 | F988AF26205D4F8B00C93F03 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F988AF24205D4F8B00C93F03 /* Main.storyboard */; }; 13 | F988AF28205D4F8B00C93F03 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F988AF27205D4F8B00C93F03 /* Assets.xcassets */; }; 14 | F988AF2B205D4F8B00C93F03 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F988AF29205D4F8B00C93F03 /* LaunchScreen.storyboard */; }; 15 | F988AF3F205D507F00C93F03 /* KSNumericTextField.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F988AF3E205D507F00C93F03 /* KSNumericTextField.framework */; }; 16 | F988AF40205D507F00C93F03 /* KSNumericTextField.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = F988AF3E205D507F00C93F03 /* KSNumericTextField.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXCopyFilesBuildPhase section */ 20 | F988AF41205D508000C93F03 /* Embed Frameworks */ = { 21 | isa = PBXCopyFilesBuildPhase; 22 | buildActionMask = 2147483647; 23 | dstPath = ""; 24 | dstSubfolderSpec = 10; 25 | files = ( 26 | F988AF40205D507F00C93F03 /* KSNumericTextField.framework in Embed Frameworks */, 27 | ); 28 | name = "Embed Frameworks"; 29 | runOnlyForDeploymentPostprocessing = 0; 30 | }; 31 | /* End PBXCopyFilesBuildPhase section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | F988AF1D205D4F8B00C93F03 /* KSNumericTextFieldExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KSNumericTextFieldExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 35 | F988AF20205D4F8B00C93F03 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 36 | F988AF22205D4F8B00C93F03 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 37 | F988AF25205D4F8B00C93F03 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 38 | F988AF27205D4F8B00C93F03 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 39 | F988AF2A205D4F8B00C93F03 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 40 | F988AF2C205D4F8B00C93F03 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 41 | F988AF3D205D505300C93F03 /* KSNumericTextField.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = KSNumericTextField.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | F988AF3E205D507F00C93F03 /* KSNumericTextField.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = KSNumericTextField.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 43 | /* End PBXFileReference section */ 44 | 45 | /* Begin PBXFrameworksBuildPhase section */ 46 | F988AF1A205D4F8B00C93F03 /* Frameworks */ = { 47 | isa = PBXFrameworksBuildPhase; 48 | buildActionMask = 2147483647; 49 | files = ( 50 | F988AF3F205D507F00C93F03 /* KSNumericTextField.framework in Frameworks */, 51 | ); 52 | runOnlyForDeploymentPostprocessing = 0; 53 | }; 54 | /* End PBXFrameworksBuildPhase section */ 55 | 56 | /* Begin PBXGroup section */ 57 | F988AF14205D4F8B00C93F03 = { 58 | isa = PBXGroup; 59 | children = ( 60 | F988AF3E205D507F00C93F03 /* KSNumericTextField.framework */, 61 | F988AF1F205D4F8B00C93F03 /* KSNumericTextFieldExample */, 62 | F988AF1E205D4F8B00C93F03 /* Products */, 63 | F988AF3B205D505300C93F03 /* Frameworks */, 64 | ); 65 | sourceTree = ""; 66 | }; 67 | F988AF1E205D4F8B00C93F03 /* Products */ = { 68 | isa = PBXGroup; 69 | children = ( 70 | F988AF1D205D4F8B00C93F03 /* KSNumericTextFieldExample.app */, 71 | ); 72 | name = Products; 73 | sourceTree = ""; 74 | }; 75 | F988AF1F205D4F8B00C93F03 /* KSNumericTextFieldExample */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | F988AF20205D4F8B00C93F03 /* AppDelegate.swift */, 79 | F988AF22205D4F8B00C93F03 /* ViewController.swift */, 80 | F988AF24205D4F8B00C93F03 /* Main.storyboard */, 81 | F988AF27205D4F8B00C93F03 /* Assets.xcassets */, 82 | F988AF29205D4F8B00C93F03 /* LaunchScreen.storyboard */, 83 | F988AF2C205D4F8B00C93F03 /* Info.plist */, 84 | ); 85 | path = KSNumericTextFieldExample; 86 | sourceTree = ""; 87 | }; 88 | F988AF3B205D505300C93F03 /* Frameworks */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | F988AF3D205D505300C93F03 /* KSNumericTextField.framework */, 92 | ); 93 | name = Frameworks; 94 | sourceTree = ""; 95 | }; 96 | /* End PBXGroup section */ 97 | 98 | /* Begin PBXNativeTarget section */ 99 | F988AF1C205D4F8B00C93F03 /* KSNumericTextFieldExample */ = { 100 | isa = PBXNativeTarget; 101 | buildConfigurationList = F988AF2F205D4F8B00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextFieldExample" */; 102 | buildPhases = ( 103 | F988AF19205D4F8B00C93F03 /* Sources */, 104 | F988AF1A205D4F8B00C93F03 /* Frameworks */, 105 | F988AF1B205D4F8B00C93F03 /* Resources */, 106 | F988AF41205D508000C93F03 /* Embed Frameworks */, 107 | ); 108 | buildRules = ( 109 | ); 110 | dependencies = ( 111 | ); 112 | name = KSNumericTextFieldExample; 113 | productName = KSNumericTextFieldExample; 114 | productReference = F988AF1D205D4F8B00C93F03 /* KSNumericTextFieldExample.app */; 115 | productType = "com.apple.product-type.application"; 116 | }; 117 | /* End PBXNativeTarget section */ 118 | 119 | /* Begin PBXProject section */ 120 | F988AF15205D4F8B00C93F03 /* Project object */ = { 121 | isa = PBXProject; 122 | attributes = { 123 | LastSwiftUpdateCheck = 0920; 124 | LastUpgradeCheck = 1010; 125 | ORGANIZATIONNAME = "Lee Kah Seng"; 126 | TargetAttributes = { 127 | F988AF1C205D4F8B00C93F03 = { 128 | CreatedOnToolsVersion = 9.2; 129 | LastSwiftMigration = 1010; 130 | ProvisioningStyle = Manual; 131 | }; 132 | }; 133 | }; 134 | buildConfigurationList = F988AF18205D4F8B00C93F03 /* Build configuration list for PBXProject "KSNumericTextFieldExample" */; 135 | compatibilityVersion = "Xcode 8.0"; 136 | developmentRegion = en; 137 | hasScannedForEncodings = 0; 138 | knownRegions = ( 139 | en, 140 | Base, 141 | ); 142 | mainGroup = F988AF14205D4F8B00C93F03; 143 | productRefGroup = F988AF1E205D4F8B00C93F03 /* Products */; 144 | projectDirPath = ""; 145 | projectRoot = ""; 146 | targets = ( 147 | F988AF1C205D4F8B00C93F03 /* KSNumericTextFieldExample */, 148 | ); 149 | }; 150 | /* End PBXProject section */ 151 | 152 | /* Begin PBXResourcesBuildPhase section */ 153 | F988AF1B205D4F8B00C93F03 /* Resources */ = { 154 | isa = PBXResourcesBuildPhase; 155 | buildActionMask = 2147483647; 156 | files = ( 157 | F988AF2B205D4F8B00C93F03 /* LaunchScreen.storyboard in Resources */, 158 | F988AF28205D4F8B00C93F03 /* Assets.xcassets in Resources */, 159 | F988AF26205D4F8B00C93F03 /* Main.storyboard in Resources */, 160 | ); 161 | runOnlyForDeploymentPostprocessing = 0; 162 | }; 163 | /* End PBXResourcesBuildPhase section */ 164 | 165 | /* Begin PBXSourcesBuildPhase section */ 166 | F988AF19205D4F8B00C93F03 /* Sources */ = { 167 | isa = PBXSourcesBuildPhase; 168 | buildActionMask = 2147483647; 169 | files = ( 170 | F988AF23205D4F8B00C93F03 /* ViewController.swift in Sources */, 171 | F988AF21205D4F8B00C93F03 /* AppDelegate.swift in Sources */, 172 | ); 173 | runOnlyForDeploymentPostprocessing = 0; 174 | }; 175 | /* End PBXSourcesBuildPhase section */ 176 | 177 | /* Begin PBXVariantGroup section */ 178 | F988AF24205D4F8B00C93F03 /* Main.storyboard */ = { 179 | isa = PBXVariantGroup; 180 | children = ( 181 | F988AF25205D4F8B00C93F03 /* Base */, 182 | ); 183 | name = Main.storyboard; 184 | sourceTree = ""; 185 | }; 186 | F988AF29205D4F8B00C93F03 /* LaunchScreen.storyboard */ = { 187 | isa = PBXVariantGroup; 188 | children = ( 189 | F988AF2A205D4F8B00C93F03 /* Base */, 190 | ); 191 | name = LaunchScreen.storyboard; 192 | sourceTree = ""; 193 | }; 194 | /* End PBXVariantGroup section */ 195 | 196 | /* Begin XCBuildConfiguration section */ 197 | F988AF2D205D4F8B00C93F03 /* Debug */ = { 198 | isa = XCBuildConfiguration; 199 | buildSettings = { 200 | ALWAYS_SEARCH_USER_PATHS = NO; 201 | CLANG_ANALYZER_NONNULL = YES; 202 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 203 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 204 | CLANG_CXX_LIBRARY = "libc++"; 205 | CLANG_ENABLE_MODULES = YES; 206 | CLANG_ENABLE_OBJC_ARC = YES; 207 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 208 | CLANG_WARN_BOOL_CONVERSION = YES; 209 | CLANG_WARN_COMMA = YES; 210 | CLANG_WARN_CONSTANT_CONVERSION = YES; 211 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 212 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 213 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 214 | CLANG_WARN_EMPTY_BODY = YES; 215 | CLANG_WARN_ENUM_CONVERSION = YES; 216 | CLANG_WARN_INFINITE_RECURSION = YES; 217 | CLANG_WARN_INT_CONVERSION = YES; 218 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 219 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 220 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 221 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 222 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 223 | CLANG_WARN_STRICT_PROTOTYPES = YES; 224 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 225 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 226 | CLANG_WARN_UNREACHABLE_CODE = YES; 227 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 228 | CODE_SIGN_IDENTITY = "iPhone Developer"; 229 | COPY_PHASE_STRIP = NO; 230 | DEBUG_INFORMATION_FORMAT = dwarf; 231 | ENABLE_STRICT_OBJC_MSGSEND = YES; 232 | ENABLE_TESTABILITY = YES; 233 | GCC_C_LANGUAGE_STANDARD = gnu11; 234 | GCC_DYNAMIC_NO_PIC = NO; 235 | GCC_NO_COMMON_BLOCKS = YES; 236 | GCC_OPTIMIZATION_LEVEL = 0; 237 | GCC_PREPROCESSOR_DEFINITIONS = ( 238 | "DEBUG=1", 239 | "$(inherited)", 240 | ); 241 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 242 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 243 | GCC_WARN_UNDECLARED_SELECTOR = YES; 244 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 245 | GCC_WARN_UNUSED_FUNCTION = YES; 246 | GCC_WARN_UNUSED_VARIABLE = YES; 247 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 248 | MTL_ENABLE_DEBUG_INFO = YES; 249 | ONLY_ACTIVE_ARCH = YES; 250 | SDKROOT = iphoneos; 251 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 252 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 253 | }; 254 | name = Debug; 255 | }; 256 | F988AF2E205D4F8B00C93F03 /* Release */ = { 257 | isa = XCBuildConfiguration; 258 | buildSettings = { 259 | ALWAYS_SEARCH_USER_PATHS = NO; 260 | CLANG_ANALYZER_NONNULL = YES; 261 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 262 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 263 | CLANG_CXX_LIBRARY = "libc++"; 264 | CLANG_ENABLE_MODULES = YES; 265 | CLANG_ENABLE_OBJC_ARC = YES; 266 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 267 | CLANG_WARN_BOOL_CONVERSION = YES; 268 | CLANG_WARN_COMMA = YES; 269 | CLANG_WARN_CONSTANT_CONVERSION = YES; 270 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 271 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 272 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 273 | CLANG_WARN_EMPTY_BODY = YES; 274 | CLANG_WARN_ENUM_CONVERSION = YES; 275 | CLANG_WARN_INFINITE_RECURSION = YES; 276 | CLANG_WARN_INT_CONVERSION = YES; 277 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 278 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 279 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 280 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 281 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 282 | CLANG_WARN_STRICT_PROTOTYPES = YES; 283 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 284 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 285 | CLANG_WARN_UNREACHABLE_CODE = YES; 286 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 287 | CODE_SIGN_IDENTITY = "iPhone Developer"; 288 | COPY_PHASE_STRIP = NO; 289 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 290 | ENABLE_NS_ASSERTIONS = NO; 291 | ENABLE_STRICT_OBJC_MSGSEND = YES; 292 | GCC_C_LANGUAGE_STANDARD = gnu11; 293 | GCC_NO_COMMON_BLOCKS = YES; 294 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 295 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 296 | GCC_WARN_UNDECLARED_SELECTOR = YES; 297 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 298 | GCC_WARN_UNUSED_FUNCTION = YES; 299 | GCC_WARN_UNUSED_VARIABLE = YES; 300 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 301 | MTL_ENABLE_DEBUG_INFO = NO; 302 | SDKROOT = iphoneos; 303 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 304 | VALIDATE_PRODUCT = YES; 305 | }; 306 | name = Release; 307 | }; 308 | F988AF30205D4F8B00C93F03 /* Debug */ = { 309 | isa = XCBuildConfiguration; 310 | buildSettings = { 311 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 312 | CODE_SIGN_STYLE = Manual; 313 | DEVELOPMENT_TEAM = QVEMCYHXHL; 314 | INFOPLIST_FILE = KSNumericTextFieldExample/Info.plist; 315 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 316 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 317 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextFieldExample; 318 | PRODUCT_NAME = "$(TARGET_NAME)"; 319 | PROVISIONING_PROFILE = "d44838de-5bb5-48fc-83dc-e0dab10bf274"; 320 | PROVISIONING_PROFILE_SPECIFIER = "yeos_lee dev profile"; 321 | SWIFT_VERSION = 4.2; 322 | TARGETED_DEVICE_FAMILY = "1,2"; 323 | }; 324 | name = Debug; 325 | }; 326 | F988AF31205D4F8B00C93F03 /* Release */ = { 327 | isa = XCBuildConfiguration; 328 | buildSettings = { 329 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 330 | CODE_SIGN_STYLE = Manual; 331 | DEVELOPMENT_TEAM = ""; 332 | INFOPLIST_FILE = KSNumericTextFieldExample/Info.plist; 333 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 334 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 335 | PRODUCT_BUNDLE_IDENTIFIER = com.LeeKahSeng.KSNumericTextFieldExample; 336 | PRODUCT_NAME = "$(TARGET_NAME)"; 337 | PROVISIONING_PROFILE_SPECIFIER = ""; 338 | SWIFT_VERSION = 4.2; 339 | TARGETED_DEVICE_FAMILY = "1,2"; 340 | }; 341 | name = Release; 342 | }; 343 | /* End XCBuildConfiguration section */ 344 | 345 | /* Begin XCConfigurationList section */ 346 | F988AF18205D4F8B00C93F03 /* Build configuration list for PBXProject "KSNumericTextFieldExample" */ = { 347 | isa = XCConfigurationList; 348 | buildConfigurations = ( 349 | F988AF2D205D4F8B00C93F03 /* Debug */, 350 | F988AF2E205D4F8B00C93F03 /* Release */, 351 | ); 352 | defaultConfigurationIsVisible = 0; 353 | defaultConfigurationName = Release; 354 | }; 355 | F988AF2F205D4F8B00C93F03 /* Build configuration list for PBXNativeTarget "KSNumericTextFieldExample" */ = { 356 | isa = XCConfigurationList; 357 | buildConfigurations = ( 358 | F988AF30205D4F8B00C93F03 /* Debug */, 359 | F988AF31205D4F8B00C93F03 /* Release */, 360 | ); 361 | defaultConfigurationIsVisible = 0; 362 | defaultConfigurationName = Release; 363 | }; 364 | /* End XCConfigurationList section */ 365 | }; 366 | rootObject = F988AF15205D4F8B00C93F03 /* Project object */; 367 | } 368 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // KSNumericTextFieldExample 4 | // 5 | // Created by Lee Kah Seng on 17/03/2018. 6 | // Copyright © 2018 Lee Kah Seng. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import KSNumericTextField 11 | 12 | @UIApplicationMain 13 | class AppDelegate: UIResponder, UIApplicationDelegate { 14 | 15 | var window: UIWindow? 16 | 17 | 18 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 19 | // Override point for customization after application launch. 20 | return true 21 | } 22 | 23 | func applicationWillResignActive(_ application: UIApplication) { 24 | // 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. 25 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 26 | } 27 | 28 | func applicationDidEnterBackground(_ application: UIApplication) { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | func applicationWillEnterForeground(_ application: UIApplication) { 34 | // 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. 35 | } 36 | 37 | func applicationDidBecomeActive(_ application: UIApplication) { 38 | // 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. 39 | } 40 | 41 | func applicationWillTerminate(_ application: UIApplication) { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | } 88 | ], 89 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /KSNumericTextFieldExample/KSNumericTextFieldExample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // KSNumericTextFieldExample 4 | // 5 | // Created by Lee Kah Seng on 17/03/2018. 6 | // Copyright © 2018 Lee Kah Seng. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import KSNumericTextField 11 | 12 | class ViewController: UIViewController { 13 | 14 | @IBOutlet weak var doubleDigitTextField: KSNumericTextField! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | 19 | // Set integer digit & fraction digit 20 | doubleDigitTextField.maxIntegerDigit = 2 21 | doubleDigitTextField.maxFractionDigit = 0 22 | } 23 | 24 | override func didReceiveMemoryWarning() { 25 | super.didReceiveMemoryWarning() 26 | // Dispose of any resources that can be recreated. 27 | } 28 | 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Lee Kah Seng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README-Assets/AttributeInspector.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeKahSeng/KSNumericTextField/c9862a2375e1a52827b9f6f3c6031ec08e9651dd/README-Assets/AttributeInspector.png -------------------------------------------------------------------------------- /README-Assets/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeeKahSeng/KSNumericTextField/c9862a2375e1a52827b9f6f3c6031ec08e9651dd/README-Assets/demo.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KSNumericTextField 2 | 3 | KSNumericTextField is a simple to use text field that only accept numeric value. You can also specify the maximum number of integer digits and faction digits using storyboard or code. 4 | 5 | 6 | ## Requirements 7 | * Xcode 10 or later 8 | * iOS 11.2 or later 9 | * Swift 4.2 or later 10 | 11 | 12 | ## Features 13 | * Easily limit number of integer digits and fraction digits 14 | * Support Xcode storyboard 15 | 16 | 17 | ## Example 18 | Clone or download the source code and launch the project with ```KSNumericTextFieldExample.xcworkspace```. The example demonstrate how to configure KSNumericTextField using storyboard or code. 19 | 20 | ![](README-Assets/demo.gif) 21 | 22 | 23 | ## Installation 24 | ### CocoaPods 25 | ``` ruby 26 | pod 'KSNumericTextField' 27 | ``` 28 | 29 | ### Carthage 30 | 1. Create and update Cartfile 31 | ``` ruby 32 | github "LeeKahSeng/KSNumericTextField" 33 | ``` 34 | 2. Build the framework using terminal 35 | ``` 36 | carthage update 37 | ``` 38 | 3. After finish building the framework using Carthage, open Xcode and select you project in the project navigator. 39 | 4. At ```Build Phases``` tab, add ```KSNumericTextField.framework``` to ```Link Binary with Libraries```. 40 | 5. At ```General``` tab, add ```KSNumericTextField.framework``` to ```Embedded Binaries```. 41 | 42 | ### Manually 43 | 1. Download the project. 44 | 2. Drag the ```Sources``` folder in ```\KSNumericTextField\KSNumericTextField``` into your Xcode project. 45 | 3. Add ```import UIKit``` to all the source code that causing compile error. 46 | 4. Build & run. 47 | 48 | ## How to use 49 | ### Storyboard 50 | 1. Drag a UITextField to your view controller. 51 | 2. Open Indentity Inspector and change text field class to KSNumericTextField. 52 | 3. Open Indentity Inspector and change text field module to KSNumericTextField. (This step only required when install using CocoaPods or Carthage) 53 | 4. Open Attribute Inspector to change 'Max Integer Digit' and 'Max Fraction Digit' base on you needs. 54 | ![](README-Assets/AttributeInspector.png) 55 | 5. Build & run. 56 | 57 | **Note: If using Carthage for installation, ```IBDesignable``` and ```IBInspectable``` will not work due Xcode limitation. Details information can be found [here](https://github.com/Carthage/Carthage/issues/335).** 58 | 59 | ### Code 60 | Import KSNumericTextField if you are using CocoaPods or Carthage. 61 | ```swift 62 | import KSNumericTextField 63 | ``` 64 | KSNumericTextField can be initialize and configure just like normal UITextField. 65 | ```swift 66 | let textField = KSNumericTextField() 67 | textField.frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 44.0) 68 | textField.maxIntegerDigit = 3 69 | textField.maxFractionDigit = 1 70 | view.addSubview(textField) 71 | ``` 72 | 73 | 74 | ## License 75 | 76 | This code is distributed under the terms and conditions of the [MIT license](LICENSE). 77 | --------------------------------------------------------------------------------