how to restrict UITextField input english number

I am trying to restrict user input to only number when user change its local to "Arabic" language,

Answered by DTS Engineer in 40221022

Take a look at the

-stringByApplyingTransform:reverse:
method, added in iOS 9. The
NSStringTransformLatinToArabic
transform will happily convert digits between (the oh-so-confusingly-named) Arabic and Eastern Arabic numerals.

If you have to support older platforms you can use

CFStringTransform
with
kCFStringTransformLatinArabic
.

btw I moved your thread to App Frameworks > Localization because this isn't a Swift-specific problem.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

How about implementing the UITextFieldDelegate method textField(_:shouldChangeCharactersInRange:replacementString:)

and returning false if the replacement string includes any non-numeric characters?

thanks for your reply, i did it like that but it will force user to chage its locale to english i don't want that, when user type any number in any language i need to convert to english number, becuase i have to forwared it api as a number

ok, there are two fields one accecpt "user name" and second "phone number", user can type name in "arabic" that is common here but for "phone number" we are accepting number in that case he has to change its local to enter "phone number" in english, while he is typing i need to convert it to "english number"

Accepted Answer

Take a look at the

-stringByApplyingTransform:reverse:
method, added in iOS 9. The
NSStringTransformLatinToArabic
transform will happily convert digits between (the oh-so-confusingly-named) Arabic and Eastern Arabic numerals.

If you have to support older platforms you can use

CFStringTransform
with
kCFStringTransformLatinArabic
.

btw I moved your thread to App Frameworks > Localization because this isn't a Swift-specific problem.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for you help, I am still using Development Target "8.0", it means i cannot access this func till update "9.0", in my development target last option is "8.4", so how can i access it

CFStringTransform
has been around since the Dawn of Time™, so you just need to wrap that in something that makes it easier to call from Swift.
extension String {

    func stringByApplyingArabicTransformingReverse(reverse: Bool) -> String {
        let selfMutable = CFStringCreateMutableCopy(nil, 0, self)
        let success = CFStringTransform(selfMutable, nil, kCFStringTransformLatinArabic, reverse)
        assert(success)
        return selfMutable as String
    }

    func stringByTransformingToArabic() -> String {
        return self.stringByApplyingArabicTransformingReverse(false)
    }

    func stringByTransformingToLatin() -> String {
        return self.stringByApplyingArabicTransformingReverse(true)
    }

}

The above was tested with Xcode 7.0b5. To get it working in Xcode 6.4 you have two tweak how it handles Booleans.

let success = CFStringTransform(selfMutable, nil, kCFStringTransformLatinArabic, reverse ? 1 : 0)
assert(success != 0)

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
how to restrict UITextField input english number
 
 
Q