IOS limit input box cannot be entered in Chinese

Source: Internet
Author: User

Development encountered this problem, think or summarize, just beginning to limit the Uitextfield keyboard for

Uikeyboardtypeasciicapable, but when the user switched the Chinese keyboard still does not solve the problem, so I added the input box to listen to the event, get input box The latest input content, detect whether the input content contains Chinese, if the Chinese is replaced with an empty string, The specific implementation is as follows:

InfoView.userTF.keyboardType = uikeyboardtypeasciicapable;

Listening for input content

[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (textfilededitchanged:)

name:@ "Uitextfieldtextdidchangenotification"

Object:infoView.userTF];

-(void) textfilededitchanged: (nsnotification*) notification

{

Uitextfield*textfield = Notification.object;

Nsstring*str = Textfield.text;

for (int i = 0; i<str.length; i++)

{

nsstring*string = [Str substringfromindex:i];

NSString *regex = @ "[\u4e00-\u9fa5]{0,}$"; Chinese

2. Stitching predicates

Nspredicate *predicatere1 = [Nspredicate predicatewithformat:@ "self matches%@", regex];

3. Match string

BOOL Resualt = [PredicateRe1 evaluatewithobject:string];

if (Resualt)

{

Is Chinese replaced with an empty string

str = [str stringbyreplacingoccurrencesofstring:[str substringfromindex:i] withstring:@ ""];

}

}

Textfield.text = str;

}

IOS limit input box cannot be entered in Chinese

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.