Check the Valid String
In Most of App we use the UITextfield for getting the value from user like their name , email or other details , some time user will make the entry of emoji so it is not a valid & we have to face lot of issue to restrict this type of string .
In iOS what we can do , we can select the ASCII Keyboard type but it will work only for English language input but it will not support for other language.
For This what we have to do.
Follow This Steps:
1: First we will take a UITextfield like Firstname
Create a Outlet of this :
@IBOutlet weak var firstNameTextField: UITextField!
2: Take a Submit Button where the user will click & get the Value of Textfield.
Here we will have to check weather the value are empty , valid .
for that write this code.
1 2 3 4 5 6 7 8 9 10 11 |
@IBAction func registerAccount(_ sender: Any) { if !firstNameTextField.isValid(name:"firstname".localized){ return }else{ // here you will get the correct value } } |
3: Now write the Extension.
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 |
extension UITextField{ func isValid(name:String)->Bool{ var errorMessage:String = NetworkManager.sharedInstance.language(key: "pleasefill")+" " if self.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" == ""{ errorMessage = errorMessage+name NetworkManager.sharedInstance.showErrorSnackBar(msg: errorMessage) // you can use any pop up controller for displaying the warning message return false }else if self.text?.containsEmoji == true{ errorMessage = errorMessage+"valid".localized+" "+name NetworkManager.sharedInstance.showErrorSnackBar(msg: errorMessage) // you can use any pop up controller for displaying the warning message return false }else{ return true } } func trimmSpace()->String{ return self.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" } } |
Note:
1: In first step we are checking the space character .
2: but in second step we are checking weather the string contains emoji or not
4: Now write this Extension for checking the string contains emoji or not .
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
extension UnicodeScalar { var isEmoji: Bool { switch value { case 0x1F600...0x1F64F, // Emoticons 0x1F300...0x1F5FF, // Misc Symbols and Pictographs 0x1F680...0x1F6FF, // Transport and Map 0x1F1E6...0x1F1FF, // Regional country flags 0x2600...0x26FF, // Misc symbols 0x2700...0x27BF, // Dingbats 0xFE00...0xFE0F, // Variation Selectors 0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs 127000...127600, // Various asian characters 65024...65039, // Variation selector 9100...9300, // Misc items 8400...8447: // Combining Diacritical Marks for Symbols return true default: return false } } var isZeroWidthJoiner: Bool { return value == 8205 } } extension String { var glyphCount: Int { let richText = NSAttributedString(string: self) let line = CTLineCreateWithAttributedString(richText) return CTLineGetGlyphCount(line) } var isSingleEmoji: Bool { return glyphCount == 1 && containsEmoji } var containsEmoji: Bool { return unicodeScalars.contains { $0.isEmoji } } var containsOnlyEmoji: Bool { return !isEmpty && !unicodeScalars.contains(where: { !$0.isEmoji && !$0.isZeroWidthJoiner }) } // The next tricks are mostly to demonstrate how tricky it can be to determine emoji's // If anyone has suggestions how to improve this, please let me know var emojiString: String { return emojiScalars.map { String($0) }.reduce("", +) } var emojis: [String] { var scalars: [[UnicodeScalar]] = [] var currentScalarSet: [UnicodeScalar] = [] var previousScalar: UnicodeScalar? for scalar in emojiScalars { if let prev = previousScalar, !prev.isZeroWidthJoiner && !scalar.isZeroWidthJoiner { scalars.append(currentScalarSet) currentScalarSet = [] } currentScalarSet.append(scalar) previousScalar = scalar } scalars.append(currentScalarSet) return scalars.map { $0.map{ String($0) } .reduce("", +) } } fileprivate var emojiScalars: [UnicodeScalar] { var chars: [UnicodeScalar] = [] var previous: UnicodeScalar? for cur in unicodeScalars { if let previous = previous, previous.isZeroWidthJoiner && cur.isEmoji { chars.append(previous) chars.append(cur) } else if cur.isEmoji { chars.append(cur) } previous = cur } return chars } } |
5: With the help of this Extension you can verified the string contains emoji or valid emoji or only emoji string .