Allow only alphanumeric characters for a UITextField
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How would I go about allowing inputting only alphanumeric characters in an iOS UITextField ? 回答1: Use the UITextFieldDelegate method -textField:shouldChangeCharactersInRange:replacementString: with an NSCharacterSet containing the inverse of the characters you want to allow. For example: // in -init, -initWithNibName:bundle:, or similar NSCharacterSet *blockedCharacters = [[[NSCharacterSet alphanumericCharacterSet] invertedSet] retain]; - (BOOL)textField:(UITextField *)field shouldChangeCharactersInRange:(NSRange)range replacementString: