Improve phone number entry

This commit is contained in:
Ilya Laktyushin
2020-08-25 17:36:15 +03:00
parent d584c0317d
commit b901cdfb0d
7 changed files with 179 additions and 42 deletions

View File

@@ -78,6 +78,7 @@ private func cleanSuffix(_ text: String) -> String {
extension String {
func applyPatternOnNumbers(pattern: String, replacementCharacter: Character) -> String {
let pattern = pattern.replacingOccurrences( of: "[0-9]", with: "X", options: .regularExpression)
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
@@ -175,8 +176,12 @@ public final class PhoneInputNode: ASDisplayNode, UITextFieldDelegate {
private func updatePlaceholder() {
if let mask = self.mask {
let mutableMask = NSMutableAttributedString(attributedString: mask)
mutableMask.replaceCharacters(in: NSRange(location: 0, length: mask.string.count), with: mask.string.replacingOccurrences(of: "X", with: "-"))
mutableMask.replaceCharacters(in: NSRange(location: 0, length: mask.string.count), with: mask.string.replacingOccurrences(of: "X", with: ""))
if let text = self.numberField.textField.text {
mutableMask.replaceCharacters(in: NSRange(location: 0, length: min(text.count, mask.string.count)), with: text)
}
mutableMask.addAttribute(.foregroundColor, value: UIColor.clear, range: NSRange(location: 0, length: min(self.numberField.textField.text?.count ?? 0, mask.string.count)))
mutableMask.addAttribute(.kern, value: 1.6, range: NSRange(location: 0, length: mask.string.count))
self.placeholderNode.attributedText = mutableMask
} else {
self.placeholderNode.attributedText = NSAttributedString(string: "")
@@ -212,7 +217,7 @@ public final class PhoneInputNode: ASDisplayNode, UITextFieldDelegate {
} else {
self.numberField.textField.keyboardType = .numberPad
}
self.numberField.textField.defaultTextAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.kern: 2.0]
self.numberField.textField.defaultTextAttributes = [NSAttributedString.Key.font: font, NSAttributedString.Key.kern: 1.6]
super.init()
self.addSubnode(self.countryCodeField)