File tree 2 files changed +17
-7
lines changed
Core/StandardLibrary/String
2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -10,10 +10,6 @@ import Foundation
10
10
11
11
extension String {
12
12
func ip_trimmed( toLength length: Int ) -> String {
13
- if ip_length > length {
14
- return substring ( to: index ( startIndex, offsetBy: length) )
15
- } else {
16
- return self
17
- }
13
+ return String ( characters. prefix ( length) )
18
14
}
19
15
}
Original file line number Diff line number Diff line change 8
8
9
9
import Foundation
10
10
import RxSwift
11
+ import RxCocoa
11
12
12
13
extension ObservableType where E == String {
13
14
public func ip_limitLength( to limit: Int ) -> Observable < E > {
@@ -21,14 +22,27 @@ extension ObservableType where E == String? {
21
22
}
22
23
}
23
24
25
+ extension ControlPropertyType where E == String {
26
+ public func ip_limited( toLength length: Int ) -> ControlProperty < String > {
27
+ let values : Observable < String > = asObservable ( ) . map { $0. ip_trimmed ( toLength: length) }
28
+ let valueSink : AnyObserver < String > = mapObserver { $0 }
29
+ return ControlProperty < String > ( values: values, valueSink: valueSink)
30
+ }
31
+ }
32
+
24
33
extension Reactive where Base: UITextField {
25
34
/** Limit the length of input to a text field
26
35
27
36
Usage Example:
28
37
29
38
textField.rx.ip_limitLength(to: 5)
30
- */
31
- public func ip_limitLength( to limit: Int ) -> Disposable {
39
+
40
+ Note: This does not restrict length when the text property of the text field is modified directly e.g.
41
+
42
+ textField.text = "A string over the limit"
43
+
44
+ */
45
+ public func ip_limitInputLength( to limit: Int ) -> Disposable {
32
46
return text. ip_limitLength ( to: limit) . bind ( to: text)
33
47
}
34
48
}
You can’t perform that action at this time.
0 commit comments