├── .gitignore ├── LICENSE ├── README.md ├── WKTextFieldFormatter.podspec ├── WKTextFieldFormatter.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── Welkin.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── macmini-bt08.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ ├── Welkin.xcuserdatad │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ │ ├── WKTextFieldFormatter.xcscheme │ │ └── xcschememanagement.plist │ └── macmini-bt08.xcuserdatad │ └── xcschemes │ ├── WKTextFieldFormatter.xcscheme │ └── xcschememanagement.plist └── WKTextFieldFormatter ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m ├── WKTextFieldFormatter ├── WKTextFieldFormatter.h └── WKTextFieldFormatter.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Welkin Xie 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##WKTextFieldFormatter 2 | ####通过简单几行代码,自动过滤用户在textField中的不合法输入。 3 | 4 | #####现已支持以下过滤方式: 5 | -- 6 | * 手机号码 7 | * 身份证号 8 | * 数字 9 | * 英文 10 | * 英文+数字 11 | * 小数 12 | * 自行设置字符集 13 | * 限制输入长度 14 | 15 | -- 16 | ![()](http://7xneqd.com1.z0.glb.clouddn.com/formatter.gif) 17 | ##使用方法 18 | 已支持CocoaPods: 19 | 20 | pod 'WKTextFieldFormatter' 21 | 22 | -- 23 | 24 | 1. 首先,把 __WKTextFieldFormatter__ 设置为controller中的一个属性: 25 | 26 | @property (strong, nonatomic) WKTextFieldFormatter *formatter; 27 | 28 | 1. 用 `initWithTextField:` 方法初始化 **_formatter** : 29 | 30 | _formatter = [[WKTextFieldFormatter alloc] initWithTextField:_textField]; 31 | 32 | 1. 设置 **_formatter** 的 __formatterType__ : 33 | 34 | typedef NS_ENUM(NSUInteger, WKFormatterType) { 35 | WKFormatterTypeAny, //不过滤 36 | WKFormatterTypePhoneNumber, //11位电话号码 37 | WKFormatterTypeNumber, //数字 38 | WKFormatterTypeDecimal, //小数,默认精确到点后两位 39 | WKFormatterTypeAlphabet, //英文字母 40 | WKFormatterTypeNumberAndAlphabet, //数字+英文字母 41 | WKFormatterTypeIDCard, //18位身份证 42 | WKFormatterTypeCustom //自定义 43 | }; 44 | 45 | 46 | 例如: 47 | 48 | _formatter.formatterType = WKFormatterTypePhoneNumber; 49 | 50 | __搞定咯。__ 51 | 52 | ##自行设置字符集 53 | 除了选择 __WKFormatterTypeCustom__ , 还要把 __characterSet__ 设置为你想要的字符集,例如: 54 | 55 | ``` 56 | _formatter.characterSet = @"iWant"; 57 | ``` 58 | 59 | ##限制输入长度 60 | 设置 __limitedLength__ 即可: 61 | 62 | ``` 63 | _formatter.limitedLength = 8; 64 | ``` 65 | 66 | ##小数精确度 67 | 默认精确到小数点后一位,可自行设置 __decimalPlace__ : 68 | 69 | ``` 70 | _formatter.decimalPlace = 2; 71 | ``` 72 | 73 | ##License 74 | WKTextFieldFormatter is released under [__MIT License__](https://github.com/WelkinXie/WKTextFieldFormatter/blob/master/LICENSE). -------------------------------------------------------------------------------- /WKTextFieldFormatter.podspec: -------------------------------------------------------------------------------- 1 | #WKTextFieldFormatter.podspec 2 | Pod::Spec.new do |s| 3 | s.name = "WKTextFieldFormatter" 4 | s.version = “2.0” 5 | s.summary = "Easily block the unwanted input to the textField." 6 | s.homepage = "https://github.com/WelkinXie/WKTextFieldFormatter" 7 | s.license = 'MIT' 8 | s.author = { "Welkin Xie" => "welkin995@126.com" } 9 | s.platform = :ios, "7.0" 10 | s.ios.deployment_target = "7.0" 11 | s.source = { :git => "https://github.com/WelkinXie/WKTextFieldFormatter.git", :tag => s.version} 12 | s.source_files = 'WKTextFieldFormatter/WKTextFieldFormatter/*.{h,m}' 13 | s.requires_arc = true 14 | end -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 2561578C1C2D31DF00D0F409 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2561578B1C2D31DF00D0F409 /* main.m */; }; 11 | 2561578F1C2D31DF00D0F409 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2561578E1C2D31DF00D0F409 /* AppDelegate.m */; }; 12 | 256157921C2D31DF00D0F409 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 256157911C2D31DF00D0F409 /* ViewController.m */; }; 13 | 256157951C2D31DF00D0F409 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 256157931C2D31DF00D0F409 /* Main.storyboard */; }; 14 | 256157971C2D31DF00D0F409 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 256157961C2D31DF00D0F409 /* Assets.xcassets */; }; 15 | 2561579A1C2D31DF00D0F409 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 256157981C2D31DF00D0F409 /* LaunchScreen.storyboard */; }; 16 | 256157A41C2D32B200D0F409 /* WKTextFieldFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 256157A31C2D32B200D0F409 /* WKTextFieldFormatter.m */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 256157871C2D31DF00D0F409 /* WKTextFieldFormatter.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WKTextFieldFormatter.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 2561578B1C2D31DF00D0F409 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 22 | 2561578D1C2D31DF00D0F409 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 23 | 2561578E1C2D31DF00D0F409 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 24 | 256157901C2D31DF00D0F409 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 25 | 256157911C2D31DF00D0F409 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 26 | 256157941C2D31DF00D0F409 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 27 | 256157961C2D31DF00D0F409 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 28 | 256157991C2D31DF00D0F409 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | 2561579B1C2D31DF00D0F409 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | 256157A21C2D32B200D0F409 /* WKTextFieldFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKTextFieldFormatter.h; sourceTree = ""; }; 31 | 256157A31C2D32B200D0F409 /* WKTextFieldFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKTextFieldFormatter.m; sourceTree = ""; }; 32 | /* End PBXFileReference section */ 33 | 34 | /* Begin PBXFrameworksBuildPhase section */ 35 | 256157841C2D31DF00D0F409 /* Frameworks */ = { 36 | isa = PBXFrameworksBuildPhase; 37 | buildActionMask = 2147483647; 38 | files = ( 39 | ); 40 | runOnlyForDeploymentPostprocessing = 0; 41 | }; 42 | /* End PBXFrameworksBuildPhase section */ 43 | 44 | /* Begin PBXGroup section */ 45 | 2561577E1C2D31DF00D0F409 = { 46 | isa = PBXGroup; 47 | children = ( 48 | 256157891C2D31DF00D0F409 /* WKTextFieldFormatter */, 49 | 256157881C2D31DF00D0F409 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | 256157881C2D31DF00D0F409 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | 256157871C2D31DF00D0F409 /* WKTextFieldFormatter.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | 256157891C2D31DF00D0F409 /* WKTextFieldFormatter */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | 256157A11C2D32B200D0F409 /* WKTextFieldFormatter */, 65 | 2561578D1C2D31DF00D0F409 /* AppDelegate.h */, 66 | 2561578E1C2D31DF00D0F409 /* AppDelegate.m */, 67 | 256157901C2D31DF00D0F409 /* ViewController.h */, 68 | 256157911C2D31DF00D0F409 /* ViewController.m */, 69 | 256157931C2D31DF00D0F409 /* Main.storyboard */, 70 | 256157961C2D31DF00D0F409 /* Assets.xcassets */, 71 | 256157981C2D31DF00D0F409 /* LaunchScreen.storyboard */, 72 | 2561579B1C2D31DF00D0F409 /* Info.plist */, 73 | 2561578A1C2D31DF00D0F409 /* Supporting Files */, 74 | ); 75 | path = WKTextFieldFormatter; 76 | sourceTree = ""; 77 | }; 78 | 2561578A1C2D31DF00D0F409 /* Supporting Files */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 2561578B1C2D31DF00D0F409 /* main.m */, 82 | ); 83 | name = "Supporting Files"; 84 | sourceTree = ""; 85 | }; 86 | 256157A11C2D32B200D0F409 /* WKTextFieldFormatter */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 256157A21C2D32B200D0F409 /* WKTextFieldFormatter.h */, 90 | 256157A31C2D32B200D0F409 /* WKTextFieldFormatter.m */, 91 | ); 92 | path = WKTextFieldFormatter; 93 | sourceTree = ""; 94 | }; 95 | /* End PBXGroup section */ 96 | 97 | /* Begin PBXNativeTarget section */ 98 | 256157861C2D31DF00D0F409 /* WKTextFieldFormatter */ = { 99 | isa = PBXNativeTarget; 100 | buildConfigurationList = 2561579E1C2D31DF00D0F409 /* Build configuration list for PBXNativeTarget "WKTextFieldFormatter" */; 101 | buildPhases = ( 102 | 256157831C2D31DF00D0F409 /* Sources */, 103 | 256157841C2D31DF00D0F409 /* Frameworks */, 104 | 256157851C2D31DF00D0F409 /* Resources */, 105 | ); 106 | buildRules = ( 107 | ); 108 | dependencies = ( 109 | ); 110 | name = WKTextFieldFormatter; 111 | productName = WKTextFieldFormatter; 112 | productReference = 256157871C2D31DF00D0F409 /* WKTextFieldFormatter.app */; 113 | productType = "com.apple.product-type.application"; 114 | }; 115 | /* End PBXNativeTarget section */ 116 | 117 | /* Begin PBXProject section */ 118 | 2561577F1C2D31DF00D0F409 /* Project object */ = { 119 | isa = PBXProject; 120 | attributes = { 121 | LastUpgradeCheck = 0720; 122 | ORGANIZATIONNAME = WelkinXie; 123 | TargetAttributes = { 124 | 256157861C2D31DF00D0F409 = { 125 | CreatedOnToolsVersion = 7.2; 126 | DevelopmentTeam = 8GMP7VS667; 127 | }; 128 | }; 129 | }; 130 | buildConfigurationList = 256157821C2D31DF00D0F409 /* Build configuration list for PBXProject "WKTextFieldFormatter" */; 131 | compatibilityVersion = "Xcode 3.2"; 132 | developmentRegion = English; 133 | hasScannedForEncodings = 0; 134 | knownRegions = ( 135 | en, 136 | Base, 137 | ); 138 | mainGroup = 2561577E1C2D31DF00D0F409; 139 | productRefGroup = 256157881C2D31DF00D0F409 /* Products */; 140 | projectDirPath = ""; 141 | projectRoot = ""; 142 | targets = ( 143 | 256157861C2D31DF00D0F409 /* WKTextFieldFormatter */, 144 | ); 145 | }; 146 | /* End PBXProject section */ 147 | 148 | /* Begin PBXResourcesBuildPhase section */ 149 | 256157851C2D31DF00D0F409 /* Resources */ = { 150 | isa = PBXResourcesBuildPhase; 151 | buildActionMask = 2147483647; 152 | files = ( 153 | 2561579A1C2D31DF00D0F409 /* LaunchScreen.storyboard in Resources */, 154 | 256157971C2D31DF00D0F409 /* Assets.xcassets in Resources */, 155 | 256157951C2D31DF00D0F409 /* Main.storyboard in Resources */, 156 | ); 157 | runOnlyForDeploymentPostprocessing = 0; 158 | }; 159 | /* End PBXResourcesBuildPhase section */ 160 | 161 | /* Begin PBXSourcesBuildPhase section */ 162 | 256157831C2D31DF00D0F409 /* Sources */ = { 163 | isa = PBXSourcesBuildPhase; 164 | buildActionMask = 2147483647; 165 | files = ( 166 | 256157A41C2D32B200D0F409 /* WKTextFieldFormatter.m in Sources */, 167 | 256157921C2D31DF00D0F409 /* ViewController.m in Sources */, 168 | 2561578F1C2D31DF00D0F409 /* AppDelegate.m in Sources */, 169 | 2561578C1C2D31DF00D0F409 /* main.m in Sources */, 170 | ); 171 | runOnlyForDeploymentPostprocessing = 0; 172 | }; 173 | /* End PBXSourcesBuildPhase section */ 174 | 175 | /* Begin PBXVariantGroup section */ 176 | 256157931C2D31DF00D0F409 /* Main.storyboard */ = { 177 | isa = PBXVariantGroup; 178 | children = ( 179 | 256157941C2D31DF00D0F409 /* Base */, 180 | ); 181 | name = Main.storyboard; 182 | sourceTree = ""; 183 | }; 184 | 256157981C2D31DF00D0F409 /* LaunchScreen.storyboard */ = { 185 | isa = PBXVariantGroup; 186 | children = ( 187 | 256157991C2D31DF00D0F409 /* Base */, 188 | ); 189 | name = LaunchScreen.storyboard; 190 | sourceTree = ""; 191 | }; 192 | /* End PBXVariantGroup section */ 193 | 194 | /* Begin XCBuildConfiguration section */ 195 | 2561579C1C2D31DF00D0F409 /* Debug */ = { 196 | isa = XCBuildConfiguration; 197 | buildSettings = { 198 | ALWAYS_SEARCH_USER_PATHS = NO; 199 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 200 | CLANG_CXX_LIBRARY = "libc++"; 201 | CLANG_ENABLE_MODULES = YES; 202 | CLANG_ENABLE_OBJC_ARC = YES; 203 | CLANG_WARN_BOOL_CONVERSION = YES; 204 | CLANG_WARN_CONSTANT_CONVERSION = YES; 205 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 206 | CLANG_WARN_EMPTY_BODY = YES; 207 | CLANG_WARN_ENUM_CONVERSION = YES; 208 | CLANG_WARN_INT_CONVERSION = YES; 209 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 210 | CLANG_WARN_UNREACHABLE_CODE = YES; 211 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 212 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 213 | COPY_PHASE_STRIP = NO; 214 | DEBUG_INFORMATION_FORMAT = dwarf; 215 | ENABLE_STRICT_OBJC_MSGSEND = YES; 216 | ENABLE_TESTABILITY = YES; 217 | GCC_C_LANGUAGE_STANDARD = gnu99; 218 | GCC_DYNAMIC_NO_PIC = NO; 219 | GCC_NO_COMMON_BLOCKS = YES; 220 | GCC_OPTIMIZATION_LEVEL = 0; 221 | GCC_PREPROCESSOR_DEFINITIONS = ( 222 | "DEBUG=1", 223 | "$(inherited)", 224 | ); 225 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 226 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 227 | GCC_WARN_UNDECLARED_SELECTOR = YES; 228 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 229 | GCC_WARN_UNUSED_FUNCTION = YES; 230 | GCC_WARN_UNUSED_VARIABLE = YES; 231 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 232 | MTL_ENABLE_DEBUG_INFO = YES; 233 | ONLY_ACTIVE_ARCH = YES; 234 | SDKROOT = iphoneos; 235 | }; 236 | name = Debug; 237 | }; 238 | 2561579D1C2D31DF00D0F409 /* Release */ = { 239 | isa = XCBuildConfiguration; 240 | buildSettings = { 241 | ALWAYS_SEARCH_USER_PATHS = NO; 242 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 243 | CLANG_CXX_LIBRARY = "libc++"; 244 | CLANG_ENABLE_MODULES = YES; 245 | CLANG_ENABLE_OBJC_ARC = YES; 246 | CLANG_WARN_BOOL_CONVERSION = YES; 247 | CLANG_WARN_CONSTANT_CONVERSION = YES; 248 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 249 | CLANG_WARN_EMPTY_BODY = YES; 250 | CLANG_WARN_ENUM_CONVERSION = YES; 251 | CLANG_WARN_INT_CONVERSION = YES; 252 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 253 | CLANG_WARN_UNREACHABLE_CODE = YES; 254 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 255 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 256 | COPY_PHASE_STRIP = NO; 257 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 258 | ENABLE_NS_ASSERTIONS = NO; 259 | ENABLE_STRICT_OBJC_MSGSEND = YES; 260 | GCC_C_LANGUAGE_STANDARD = gnu99; 261 | GCC_NO_COMMON_BLOCKS = YES; 262 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 264 | GCC_WARN_UNDECLARED_SELECTOR = YES; 265 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 266 | GCC_WARN_UNUSED_FUNCTION = YES; 267 | GCC_WARN_UNUSED_VARIABLE = YES; 268 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 269 | MTL_ENABLE_DEBUG_INFO = NO; 270 | SDKROOT = iphoneos; 271 | VALIDATE_PRODUCT = YES; 272 | }; 273 | name = Release; 274 | }; 275 | 2561579F1C2D31DF00D0F409 /* Debug */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 279 | CODE_SIGN_IDENTITY = "iPhone Developer"; 280 | INFOPLIST_FILE = WKTextFieldFormatter/Info.plist; 281 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 282 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 283 | PRODUCT_BUNDLE_IDENTIFIER = com.welkinx.formatter; 284 | PRODUCT_NAME = "$(TARGET_NAME)"; 285 | }; 286 | name = Debug; 287 | }; 288 | 256157A01C2D31DF00D0F409 /* Release */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 292 | CODE_SIGN_IDENTITY = "iPhone Developer"; 293 | INFOPLIST_FILE = WKTextFieldFormatter/Info.plist; 294 | IPHONEOS_DEPLOYMENT_TARGET = 7.0; 295 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 296 | PRODUCT_BUNDLE_IDENTIFIER = com.welkinx.formatter; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | }; 299 | name = Release; 300 | }; 301 | /* End XCBuildConfiguration section */ 302 | 303 | /* Begin XCConfigurationList section */ 304 | 256157821C2D31DF00D0F409 /* Build configuration list for PBXProject "WKTextFieldFormatter" */ = { 305 | isa = XCConfigurationList; 306 | buildConfigurations = ( 307 | 2561579C1C2D31DF00D0F409 /* Debug */, 308 | 2561579D1C2D31DF00D0F409 /* Release */, 309 | ); 310 | defaultConfigurationIsVisible = 0; 311 | defaultConfigurationName = Release; 312 | }; 313 | 2561579E1C2D31DF00D0F409 /* Build configuration list for PBXNativeTarget "WKTextFieldFormatter" */ = { 314 | isa = XCConfigurationList; 315 | buildConfigurations = ( 316 | 2561579F1C2D31DF00D0F409 /* Debug */, 317 | 256157A01C2D31DF00D0F409 /* Release */, 318 | ); 319 | defaultConfigurationIsVisible = 0; 320 | defaultConfigurationName = Release; 321 | }; 322 | /* End XCConfigurationList section */ 323 | }; 324 | rootObject = 2561577F1C2D31DF00D0F409 /* Project object */; 325 | } 326 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/project.xcworkspace/xcuserdata/Welkin.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WelkinXie/WKTextFieldFormatter/4a91ff2243cfa3e9189a12bc5561ff1794d565df/WKTextFieldFormatter.xcodeproj/project.xcworkspace/xcuserdata/Welkin.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/project.xcworkspace/xcuserdata/macmini-bt08.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WelkinXie/WKTextFieldFormatter/4a91ff2243cfa3e9189a12bc5561ff1794d565df/WKTextFieldFormatter.xcodeproj/project.xcworkspace/xcuserdata/macmini-bt08.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/xcuserdata/Welkin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/xcuserdata/Welkin.xcuserdatad/xcschemes/WKTextFieldFormatter.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/xcuserdata/Welkin.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | WKTextFieldFormatter.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 256157861C2D31DF00D0F409 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/xcuserdata/macmini-bt08.xcuserdatad/xcschemes/WKTextFieldFormatter.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /WKTextFieldFormatter.xcodeproj/xcuserdata/macmini-bt08.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | WKTextFieldFormatter.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 256157861C2D31DF00D0F409 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // WKTextFieldFormatter 4 | // 5 | // Created by Welkin Xie on 15/12/25. 6 | // Copyright © 2015年 WelkinXie. All rights reserved. 7 | // 8 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 9 | // 10 | 11 | #import 12 | 13 | @interface AppDelegate : UIResponder 14 | 15 | @property (strong, nonatomic) UIWindow *window; 16 | 17 | 18 | @end 19 | 20 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // WKTextFieldFormatter 4 | // 5 | // Created by Welkin Xie on 15/12/25. 6 | // Copyright © 2015年 WelkinXie. All rights reserved. 7 | // 8 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 9 | // 10 | 11 | #import "AppDelegate.h" 12 | 13 | @interface AppDelegate () 14 | 15 | @end 16 | 17 | @implementation AppDelegate 18 | 19 | 20 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 21 | // Override point for customization after application launch. 22 | return YES; 23 | } 24 | 25 | - (void)applicationWillResignActive:(UIApplication *)application { 26 | // 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. 27 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 28 | } 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // 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. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | - (void)applicationWillEnterForeground:(UIApplication *)application { 36 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 37 | } 38 | 39 | - (void)applicationDidBecomeActive:(UIApplication *)application { 40 | // 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. 41 | } 42 | 43 | - (void)applicationWillTerminate:(UIApplication *)application { 44 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 45 | } 46 | 47 | @end 48 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /WKTextFieldFormatter/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 | 27 | 28 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/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 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 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 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // WKTextFieldFormatter 4 | // 5 | // Created by Welkin Xie on 15/12/25. 6 | // Copyright © 2015年 WelkinXie. All rights reserved. 7 | // 8 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 9 | // 10 | 11 | #import 12 | 13 | @interface ViewController : UIViewController 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // WKTextFieldFormatter 4 | // 5 | // Created by Welkin Xie on 15/12/25. 6 | // Copyright © 2015年 WelkinXie. All rights reserved. 7 | // 8 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 9 | // 10 | 11 | #import "ViewController.h" 12 | #import "WKTextFieldFormatter.h" 13 | 14 | @interface ViewController () 15 | 16 | @property (weak, nonatomic) IBOutlet UITextField *textField; 17 | @property (strong, nonatomic) WKTextFieldFormatter *formatter; 18 | 19 | @end 20 | 21 | @implementation ViewController 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | 26 | _formatter = [[WKTextFieldFormatter alloc] initWithTextField:_textField]; 27 | _formatter.formatterType = WKFormatterTypeDecimal; 28 | _formatter.decimalPlace = 2; 29 | 30 | [_textField becomeFirstResponder]; 31 | } 32 | 33 | - (void)didReceiveMemoryWarning { 34 | [super didReceiveMemoryWarning]; 35 | // Dispose of any resources that can be recreated. 36 | } 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/WKTextFieldFormatter/WKTextFieldFormatter.h: -------------------------------------------------------------------------------- 1 | // 2 | // WKTextFieldFormatter.h 3 | // 4 | // Created by Welkin Xie on 15/12/25. 5 | // Copyright © 2015年 WelkinXie. All rights reserved. 6 | // 7 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 8 | // 9 | 10 | #import 11 | #import 12 | 13 | typedef NS_ENUM(NSUInteger, WKFormatterType) { 14 | WKFormatterTypeAny, //不过滤 15 | WKFormatterTypePhoneNumber, //11位电话号码 16 | WKFormatterTypeNumber, //数字 17 | WKFormatterTypeDecimal, //小数 18 | WKFormatterTypeAlphabet, //英文字母 19 | WKFormatterTypeNumberAndAlphabet, //数字+英文字母 20 | WKFormatterTypeIDCard, //18位身份证 21 | WKFormatterTypeCustom //自定义 22 | }; 23 | 24 | @interface WKTextFieldFormatter : NSObject 25 | 26 | //格式类型 27 | @property (assign, nonatomic) WKFormatterType formatterType; 28 | 29 | //限制长度 30 | @property (assign, nonatomic) NSUInteger limitedLength; 31 | 32 | //允许的字符集 33 | @property (copy, nonatomic) NSString *characterSet; 34 | 35 | //小数位 36 | @property (assign, nonatomic) NSUInteger decimalPlace; 37 | 38 | - (instancetype)initWithTextField:(UITextField *)textField; 39 | 40 | @end -------------------------------------------------------------------------------- /WKTextFieldFormatter/WKTextFieldFormatter/WKTextFieldFormatter.m: -------------------------------------------------------------------------------- 1 | // 2 | // WKTextFieldFormatter.m 3 | // 4 | // Created by Welkin Xie on 15/12/25. 5 | // Copyright © 2015年 WelkinXie. All rights reserved. 6 | // 7 | // Github: https://github.com/WelkinXie/WKTextFieldFormatter 8 | // 9 | 10 | #import "WKTextFieldFormatter.h" 11 | 12 | @interface WKTextFieldFormatter () 13 | 14 | @property (copy, nonatomic) NSString *currentText; 15 | 16 | @end 17 | 18 | @implementation WKTextFieldFormatter 19 | 20 | - (instancetype)initWithTextField:(UITextField *)textField { 21 | if (self = [self init]) { 22 | _formatterType = WKFormatterTypeAny; 23 | _limitedLength = INT16_MAX; 24 | _characterSet = @""; 25 | _decimalPlace = 1; 26 | _currentText = @""; 27 | 28 | [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; 29 | } 30 | return self; 31 | } 32 | 33 | - (void)textChanged:(UITextField *)textField { 34 | NSString *regexString = @""; 35 | switch (_formatterType) { 36 | case WKFormatterTypeAny: 37 | { 38 | _currentText = textField.text; 39 | return; 40 | } 41 | case WKFormatterTypePhoneNumber: 42 | { 43 | regexString = @"^\\d{0,11}$"; 44 | break; 45 | } 46 | case WKFormatterTypeNumber: 47 | { 48 | regexString = @"^\\d*$"; 49 | break; 50 | } 51 | case WKFormatterTypeDecimal: 52 | { 53 | regexString = [NSString stringWithFormat:@"^(\\d+)\\.?(\\d{0,%lu})$", _decimalPlace]; 54 | break; 55 | } 56 | case WKFormatterTypeAlphabet: 57 | { 58 | regexString = @"^[a-zA-Z]*$"; 59 | break; 60 | } 61 | case WKFormatterTypeNumberAndAlphabet: 62 | { 63 | regexString = @"^[a-zA-Z0-9]*$"; 64 | break; 65 | } 66 | case WKFormatterTypeIDCard: 67 | { 68 | regexString = @"^\\d{1,17}[0-9Xx]?$"; 69 | break; 70 | } 71 | case WKFormatterTypeCustom: 72 | { 73 | regexString = [NSString stringWithFormat:@"^[%@]{0,%lu}$", _characterSet, _limitedLength]; 74 | break; 75 | } 76 | default: 77 | break; 78 | } 79 | NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString]; 80 | if (!([regexTest evaluateWithObject:textField.text] || textField.text.length == 0)) { 81 | textField.text = self.currentText; 82 | } else { 83 | self.currentText = textField.text; 84 | } 85 | } 86 | 87 | @end 88 | -------------------------------------------------------------------------------- /WKTextFieldFormatter/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // WKTextFieldFormatter 4 | // 5 | // Created by Welkin Xie on 15/12/25. 6 | // Copyright © 2015年 WelkinXie. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------