Skip to content

Commit 93bc50b

Browse files
committed
modify to SimpleInput
1 parent 8fb8801 commit 93bc50b

File tree

20 files changed

+928
-898
lines changed

20 files changed

+928
-898
lines changed

Demo/LimitedViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class LimitedViewController: UIViewController {
4343
textField.limitedInput.processChangeClosure { text in
4444
print("实时文本:\(text ?? "")")
4545
}
46+
textField.limitedInput.processRealChangeClosure { text in
47+
print("真实文本:\(text ?? "")")
48+
}
4649
}
4750

4851
public override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

PerfectInput.xcodeproj/project.pbxproj

Lines changed: 0 additions & 873 deletions
This file was deleted.

Podfile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# Uncomment the next line to define a global platform for your project
22
platform :ios, '10.2'
3-
4-
target 'PerfectInput' do
3+
target 'SimpleInput' do
54
# Comment the next line if you don't want to use dynamic frameworks
65
use_frameworks!
76
pod 'SnapKit'
8-
# Pods for PerfectInput
7+
# Pods for SimpleInput
98

10-
target 'PerfectInputTests' do
9+
target 'SimpleInputTests' do
1110
inherit! :search_paths
1211
# Pods for testing
1312
end
1413

15-
target 'PerfectInputUITests' do
14+
target 'SimpleInputUITests' do
1615
# Pods for testing
1716
end
1817

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ SPEC REPOS:
1111
SPEC CHECKSUMS:
1212
SnapKit: 97b92857e3df3a0c71833cce143274bf6ef8e5eb
1313

14-
PODFILE CHECKSUM: 36b500b001bc75b828b4636f7c466e2ef2947904
14+
PODFILE CHECKSUM: e866a4bf7e90baf5814c0a9001a86e793aa29a33
1515

1616
COCOAPODS: 1.10.1

SimpleInput.xcodeproj/project.pbxproj

Lines changed: 873 additions & 0 deletions
Large diffs are not rendered by default.

PerfectInput.xcworkspace/contents.xcworkspacedata renamed to SimpleInput.xcworkspace/contents.xcworkspacedata

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PerfectInput/AppDelegate.swift renamed to SimpleInput/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// AppDelegate.swift
3-
// PerfectInput
3+
// SimpleInput
44
//
55
// Created by galaxy on 2022/2/8.
66
//
@@ -22,4 +22,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2222
return true
2323
}
2424
}
25-
File renamed without changes.

PerfectInputTests/PerfectInputTests.swift renamed to SimpleInputTests/SimpleInputTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//
2-
// PerfectInputTests.swift
3-
// PerfectInputTests
2+
// SimpleInputTests.swift
3+
// SimpleInputTests
44
//
55
// Created by galaxy on 2022/2/8.
66
//
77

88
import XCTest
9-
@testable import PerfectInput
9+
@testable import SimpleInput
1010

11-
class PerfectInputTests: XCTestCase {
11+
class SimpleInputTests: XCTestCase {
1212

1313
override func setUpWithError() throws {
1414
// Put setup code here. This method is called before the invocation of each test method in the class.

PerfectInputUITests/PerfectInputUITests.swift renamed to SimpleInputUITests/SimpleInputUITests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// PerfectInputUITests.swift
3-
// PerfectInputUITests
2+
// SimpleInputUITests.swift
3+
// SimpleInputUITests
44
//
55
// Created by galaxy on 2022/2/8.
66
//
77

88
import XCTest
99

10-
class PerfectInputUITests: XCTestCase {
10+
class SimpleInputUITests: XCTestCase {
1111

1212
override func setUpWithError() throws {
1313
// Put setup code here. This method is called before the invocation of each test method in the class.

PerfectInputUITests/PerfectInputUITestsLaunchTests.swift renamed to SimpleInputUITests/SimpleInputUITestsLaunchTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// PerfectInputUITestsLaunchTests.swift
3-
// PerfectInputUITests
2+
// SimpleInputUITestsLaunchTests.swift
3+
// SimpleInputUITests
44
//
55
// Created by galaxy on 2022/2/8.
66
//
77

88
import XCTest
99

10-
class PerfectInputUITestsLaunchTests: XCTestCase {
10+
class SimpleInputUITestsLaunchTests: XCTestCase {
1111

1212
override class var runsForEachTargetApplicationUIConfiguration: Bool {
1313
true

Sources/Limited/LimitedInput.swift

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class LimitedInput {
1515
}
1616

1717
private var changeClosures:[(String?)->()] = []
18+
private var realChangeClosures:[(String?)->()] = []
1819
private var hasAddObserver = false
1920
private var _object: _Object?
2021

@@ -50,17 +51,17 @@ public class LimitedInput {
5051
}
5152
}
5253

53-
/// 获取输入框的内容
54+
/// 获取输入框的内容(已对小数策略做了处理)
5455
public var text: String? {
5556
guard var sText = getText() else { return nil }
5657
// 针对小数策略,做特殊处理
57-
if let decimalPolicy = decimalPolicy {
58-
// 首先去除尾部的0和小数点
58+
if let _ = decimalPolicy {
59+
// 去除尾部的0和小数点,比如输入框的内容为`0.120`或者`5.`
5960
let groups = sText.components(separatedBy: String.dot)
6061
if groups.count == 2 {
6162
var s = groups[1]
6263
while true {
63-
if s.hasPrefix(String.zero) {
64+
if s.hasSuffix(String.zero) {
6465
s = String(s.prefix(s.count - 1))
6566
} else {
6667
break
@@ -72,6 +73,22 @@ public class LimitedInput {
7273
sText = groups[0] + String.dot + s
7374
}
7475
}
76+
if sText.hasSignedPrefix {
77+
// 针对输入框只有`+`或者`-`的情况
78+
let after = String(sText.suffix(sText.count - 1))
79+
if after.count <= 0 {
80+
return nil
81+
}
82+
}
83+
}
84+
return sText
85+
}
86+
87+
/// 获取真实文本内容(针对小数策略做了特殊处理)
88+
public var realText: String? {
89+
guard let sText = text else { return nil }
90+
// 针对小数策略,做特殊处理
91+
if let decimalPolicy = decimalPolicy {
7592
// 拆分单元
7693
var prefix = ""
7794
var afterText = sText
@@ -184,8 +201,13 @@ public class LimitedInput {
184201
} else if let generalPolicy = generalPolicy {
185202
checkGeneral(generalPolicy: generalPolicy)
186203
}
187-
for f in changeClosures {
188-
f(text)
204+
DispatchQueue.main.async {
205+
for f in self.changeClosures {
206+
f(self.text)
207+
}
208+
for f in self.realChangeClosures {
209+
f(self.realText)
210+
}
189211
}
190212
}
191213

@@ -194,10 +216,17 @@ public class LimitedInput {
194216
changeClosures.append(closure!)
195217
}
196218
}
219+
220+
public func processRealChangeClosure(_ closure:((String?)->())?) {
221+
if closure != nil {
222+
realChangeClosures.append(closure!)
223+
}
224+
}
197225
}
198226

199227
extension LimitedInput {
200228
private func startObserver() {
229+
updateInput()
201230
guard let _object = self._object else { return }
202231
if hasAddObserver { return }
203232
// 将整个见天代码放在一个Block里面,这样设置,即使text赋值在之前,也能完美监听

0 commit comments

Comments
 (0)