From 18d7304fb35408ad9f6f146e78983649852a8460 Mon Sep 17 00:00:00 2001 From: Darren Ehlers Date: Tue, 1 Sep 2020 16:14:52 -0500 Subject: [PATCH] =?UTF-8?q?Added=20option=20for=20AnimationFieldFormat=20t?= =?UTF-8?q?o=20configure=20=E2=80=9CinvalidCharacters=E2=80=9D=20that=20wi?= =?UTF-8?q?ll=20be=20blocked/filtered=20from=20the=20textField/textView?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnimatedField+TextFieldDelegate.swift | 21 +++++++++---------- .../AnimatedField+TextViewDelegate.swift | 21 +++++++++---------- .../Classes/AnimatedFieldFormat.swift | 7 +++++-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/AnimatedField/Classes/AnimatedField+TextFieldDelegate.swift b/AnimatedField/Classes/AnimatedField+TextFieldDelegate.swift index 94c37ae..3281aba 100644 --- a/AnimatedField/Classes/AnimatedField+TextFieldDelegate.swift +++ b/AnimatedField/Classes/AnimatedField+TextFieldDelegate.swift @@ -19,17 +19,16 @@ extension AnimatedField: UITextFieldDelegate { // Copy new character var newInput = string - - // Replace special characters in newInput - newInput = newInput.replacingOccurrences(of: "`", with: "") - newInput = newInput.replacingOccurrences(of: "^", with: "") - newInput = newInput.replacingOccurrences(of: "¨", with: "") - - // Replace special characters in textField - textField.text = textField.text?.replacingOccurrences(of: "`", with: "") - textField.text = textField.text?.replacingOccurrences(of: "^", with: "") - textField.text = textField.text?.replacingOccurrences(of: "¨", with: "") - + + let invalidCharacters = format.invalidCharacters.map { String($0) } + for invalidCharacter in invalidCharacters { + // Replace special characters in newInput + newInput = newInput.replacingOccurrences(of: invalidCharacter, with: "") + + // Replace special characters in textField + textField.text = textField.text?.replacingOccurrences(of: invalidCharacter, with: "") + } + // Apply uppercased & lowercased if available if uppercased { newInput = newInput.uppercased() } if lowercased { newInput = newInput.lowercased() } diff --git a/AnimatedField/Classes/AnimatedField+TextViewDelegate.swift b/AnimatedField/Classes/AnimatedField+TextViewDelegate.swift index 7214e89..c08a977 100644 --- a/AnimatedField/Classes/AnimatedField+TextViewDelegate.swift +++ b/AnimatedField/Classes/AnimatedField+TextViewDelegate.swift @@ -18,17 +18,16 @@ extension AnimatedField: UITextViewDelegate { // Copy new character var newInput = text - - // Replace special characters in newInput - newInput = newInput.replacingOccurrences(of: "`", with: "") - newInput = newInput.replacingOccurrences(of: "^", with: "") - newInput = newInput.replacingOccurrences(of: "¨", with: "") - - // Replace special characters in textView - textView.text = textView.text?.replacingOccurrences(of: "`", with: "") - textView.text = textView.text?.replacingOccurrences(of: "^", with: "") - textView.text = textView.text?.replacingOccurrences(of: "¨", with: "") - + + let invalidCharacters = format.invalidCharacters.map { String($0) } + for invalidCharacter in invalidCharacters { + // Replace special characters in newInput + newInput = newInput.replacingOccurrences(of: invalidCharacter, with: "") + + // Replace special characters in textView + textView.text = textView.text?.replacingOccurrences(of: invalidCharacter, with: "") + } + // Apply uppercased & lowercased if available if uppercased { newInput = newInput.uppercased() } if lowercased { newInput = newInput.lowercased() } diff --git a/AnimatedField/Classes/AnimatedFieldFormat.swift b/AnimatedField/Classes/AnimatedFieldFormat.swift index b039713..7bf32bc 100644 --- a/AnimatedField/Classes/AnimatedFieldFormat.swift +++ b/AnimatedField/Classes/AnimatedFieldFormat.swift @@ -9,10 +9,13 @@ import Foundation public struct AnimatedFieldFormat { - + + /// String of characters to block/filter from input + public var invalidCharacters = "`^¨" + /// Title always visible public var titleAlwaysVisible = false - + /// Font for title label public var titleFont = UIFont.systemFont(ofSize: 13, weight: .regular)