-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextFieldHelper.swift
34 lines (30 loc) · 1.06 KB
/
TextFieldHelper.swift
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
29
30
31
32
33
34
//
// TextFieldHelper.swift
// PDKit
//
// Created by Juliya Smith on 3/15/21.
// Copyright © 2021 Juliya Smith. All rights reserved.
//
import Foundation
public class TextFieldHelper {
/// Determines if the updated text is okay to be set by not exceeding the max size.
/// Returns a boolean indicating if it can be set as well as the updated text after replacement.
public static func canSet(
currentString: String,
replacementString: String,
range: NSRange,
max: Int = SanitationConstants.MaxResourceNameCharacters
) -> (canReplace: Bool, updatedText: String) {
let newString = (currentString as NSString).replacingCharacters(
in: range, with: replacementString
)
let canSet = newString.count <= max
if canSet, let textRange = Range(range, in: currentString) {
let newString = currentString.replacingCharacters(
in: textRange, with: replacementString
)
return (true, newString)
}
return (false, currentString)
}
}