How to Restrict Emoji Input and Enforce Character Length in Swift (with UITextField & UITextView)

Dreamcoder
2 min readMar 20, 2025

--

๐Ÿ“˜ Overview

When building iOS apps, you often want to:

  • ๐Ÿšซ Prevent users from typing emoji characters
  • โœ… Allow only specific inputs like numbers, #, *
  • ๐Ÿ“ Count string length accurately (UTF-8 or UTF-16)
  • โŒจ๏ธ Enforce ASCII-capable keyboards only

In this blog, youโ€™ll learn how to do all of that in a clean and reusable way using String extensions, UITextFieldDelegate, and UITextViewDelegate.

โœ… Goals

  • โŒ Disallow emojis in UITextField and UITextView
  • โœ… Allow special characters like 0-9, #, *
  • ๐Ÿง  Detect input length by utf8.count or utf16.count
  • โŒจ๏ธ Force ASCII-capable keyboard

๐Ÿ’ก 1. Create String Extension

import Foundation

extension String {

var utf8Length: Int {
return self.utf8.count
}

var utf16Length: Int {
return self.utf16.count
}

var hasEmoji: Bool {
return self.unicodeScalars.contains { scalar in
switch scalar.value {
case 0x30...0x39, // '0' to '9' should not be considered emoji i.e. number
0x0023, // '#'
0x002A: // '*'
return false
case 0x1F600...0x1F64F, // Emoticons (e.g., ๐Ÿ˜€๐Ÿ˜ƒ๐Ÿ˜„๐Ÿ˜๐Ÿ˜†)
0x1F300...0x1F5FF, // Misc Symbols and Pictographs (e.g., ๐ŸŒธ๐ŸŽ‰๐Ÿ’ก๐Ÿšฟ๐Ÿ“š)
0x1F680...0x1F6FF, // Transport and Map Symbols (e.g., ๐Ÿš—โœˆ๏ธ๐Ÿš€๐Ÿš‰โ›ต๏ธ)
0x1F1E6...0x1F1FF, // Regional Indicator Symbols (used for country flags ๐Ÿ‡ฎ๐Ÿ‡ณ๐Ÿ‡บ๐Ÿ‡ธ)
0x2600...0x26FF, // Misc Symbols (e.g., โ˜€๏ธโ˜๏ธโ˜‚๏ธโ˜น๏ธโ™ป๏ธโ™ ๏ธ)
0x2700...0x27BF, // Dingbats (e.g., โœ‚๏ธโœˆ๏ธโœ‰๏ธโš ๏ธโ˜‘๏ธ)
0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs (e.g., ๐Ÿค–๐Ÿฆ„๐Ÿฅ‘๐Ÿง ๐Ÿฆท)
0x1F018...0x1F270, // Various emoji-like symbols (e.g., ๐Ÿ€„๏ธ๐Ÿƒ๐Ÿ”ž๐Ÿ•)
0x238C...0x2454, // Misc technical symbols (e.g., โฐโณโญ๏ธโฎ๏ธโธ๏ธ)
0x20D0...0x20FF: // Combining Diacritical Marks for Symbols (variation selectors like โƒ ๏ธ)
return true
default:
return scalar.properties.isEmoji // Fallback check for any other emoji
}
}
}
}

๐Ÿ’ก 2. Enforce Emoji Restriction in UITextField & UITextView

extension ViewController: UITextViewDelegate, UITextFieldDelegate {

func textView(_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool {
return !text.hasEmoji
}

func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {

// Restrict emojis in specific text field
if textField == specificTextField {
return !string.hasEmoji
}

return true
}
}

๐Ÿ’ก 3. Force ASCII Keyboard Type

override func viewDidLoad() {
super.viewDidLoad()

specificTextField.delegate = self
abcTextView.delegate = self

specificTextField.keyboardType = .asciiCapable
abcTextView.keyboardType = .asciiCapable
}

๐Ÿ’ก 4. Dismiss Keyboard on Tap Outside (Optional UX Tip)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

๐Ÿงช Test It Out

debugPrint("Hello".hasEmoji)         // false
debugPrint("123".hasEmoji) // false
debugPrint("speical โœˆ๏ธ".hasEmoji) // true
debugPrint("Hello ๐Ÿ˜ƒ".hasEmoji) // true

โœจ Bonus: Length Validation Example

if inviteNameTextField.text?.utf16Length ?? 0 > 160 {
print("Text exceeds UTF-16 byte limit")
}

๐Ÿ”š Conclusion

With a few lines of reusable code:

  • You protect your inputs from emojis
  • Maintain database-safe character limits
  • Ensure cleaner UX and backend consistency

This is ideal for forms, signups, chat inputs, payment fields, or any strict validation flow in your app.

๐Ÿ”– Summary Checklist

โœ… Emoji check in String extension
โœ… Limit text input using UITextFieldDelegate & UITextViewDelegate
โœ… Enforce .asciiCapable keyboard
โœ… Optional: Limit text length using .utf16Length

Appreciate you going through this โ€” if it helped in any way, feel free to like or share!

--

--

No responses yet