PennyWise
PennyWise

Reputation: 647

How to allow emoji in UITextField

I am limiting the input in a UITextField to only characters from the alphabet like this in shouldChangeCharactersIn:

switch textField {
    case descriptionTextField:
        return prospectiveText.containsOnlyCharactersIn(matchCharacters: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZáäâàæãåāéëêèęėēóôòõœøōúüûùūíïìîįīÁÄÂÀÆÃÅĀÓÖÔÒÕŒØŌÚÜÛÙŪÍÏÌÎĮĪ ") && prospectiveText.count <= 51
    default:
        return true
}

Now while this works like a charm, I want to allow users to insert emoji as well. Is there any way I can accomplish this? I don't want them to insert numbers or special characters though.

Upvotes: 0

Views: 858

Answers (1)

AtulParmar
AtulParmar

Reputation: 4570

Simply check reverse condition, in condition, include the number and all special characters, and put '!' before prospectiveText.containsOnlyCharactersIn so if found any number and special character which is you have specified then it will return false.

switch textField {
    case descriptionTextField:
        return !prospectiveText.containsOnlyCharactersIn(matchCharacters: "<put here numbers and all your symbols you want to ignore> ") && prospectiveText.count <= 51
    default:
        return true
}

Upvotes: 0

Related Questions