4

I'm using the following code to limit the amount of characters that can be in a UITextField

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

        let currentCharacterCount = textField.text?.characters.count ?? 0
        if (range.length + range.location > currentCharacterCount){
            return false
        }
        let newLength = currentCharacterCount + string.characters.count - range.length
        return newLength <= 44
    }

This is done by setting the UITextField's delegate to self.

--

The issue here is that if you add an Emoji to the textfield, you cannot remove it. Even if you highlight - select all, or use "cut" the text will not change. The emoji can be before or after text, or even alone in the text field. You also cannot add two emojis to the field.

I don't see what the issue is here, can anyone help me out?

Vvk
  • 4,031
  • 29
  • 51
Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • Need clarification- 1. First do you want to restrict a single emoji in UItextfeild? 2. You are trying to delete the text in textfeild but the emoji remains but the text get deleted. Is that right? – Vizllx Feb 22 '16 at 06:03

2 Answers2

0

Its better if you don't do any checks inside shouldChange.. function and instead track the length separately using the UITextFieldTextDidChangeNotification notification. Sample code is attached below. In the shouldChange..just return a bool which is set in textDidChange. Here you will get the correct length. I noticed that in your method once you add an emoji it wont even allow you to type anymore. The shouldChangeCharactersInRange is not even getting called after entering a emoji char.

class ViewController: UIViewController, UITextFieldDelegate{

    @IBOutlet weak var textField:UITextField!
    var allow = true

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldDidChange:", name: UITextFieldTextDidChangeNotification, object: textField)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

     func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        return allow
    }

    func textFieldDidChange(notification:NSNotification)
    {
        let length = textField.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        if length > 44 {
            allow = false
        } else {
            allow = true
        }
    }

}
Pradeep K
  • 3,671
  • 1
  • 11
  • 15
  • Thank you for this, while it does help, it still manages to hang at random. Sometimes it could be 2 or 3 emojis, sometimes it can be 4-5. I understand that emojis take up 6 characters towards the limit, so that's not the issue here. The same problem persists, whereas you cannot add text, or delete anything, the entire textfield just becomes dead. – Hobbyist Feb 22 '16 at 06:41
0

In Swift 3.1

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let currentCharacterCount = textField.text?.characters.count ?? 0
    let limit = 10
    return currentCharacterCount < limit || string.characters.count < range.length
}

This won't prevent copy and paste long text string, I disabled the paste function for UITextField, with codes from

https://stackoverflow.com/a/39015132/6311644

Jayce
  • 427
  • 5
  • 6