Today, the project used this to write, convenient for later students can not be used directly
First, to rewrite the uitextfiled subclass under the initialization method, copy the following method to disable the long press of the menu that appears
kill TextField 's long-press menu to disable copy paste
-(BOOL) canperformaction: (SEL) Action Withsender: (ID) Sender
{
if ([uimenucontrollersharedmenucontroller]) {
[uimenucontrollersharedmenucontroller]. Menuvisible =NO;
}
return NO;
}
In development we sometimes do some work on uitextfiled, say how to enter only Chinese, or just enter numbers and methods in English 2
I define the definition directly with a macro:
#define Kalphanum @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"//This is the corresponding only input Chinese does not allow other numbers or symbols
#define Kenglishnum @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"// This is the only way to enter numbers and English
Delegate method of executing Uitextfield remember textfiled.delegate = self;
Directly below the code:
-(BOOL) TextField: (uitextfield *) TextField shouldchangecharactersinrange: (nsrange) Range replacementstring: (nsstring *) string
{
Uitextfield *namefield = (uitextfield *) [selfviewwithtag:+];
Uitextfield *cardfield = (uitextfield *) [selfviewwithtag:201];
if (TextField = = NameField) {
// allow only Chinese input
Nscharacterset *cs;
cs = [[nscharactersetcharactersetwithcharactersinstring:kalphanum ]invertedset];
nsstring *filtered = [[StringComponentsseparatedbycharactersinset: cs] Comp Onentsjoinedbystring:@ ""];
BOOL basic = [string isequaltostring: filtered];
nsstring *text = [TextField. Textstringbyreplacingcharactersinrange: Range withstring: string];
if (_delegate && [_delegaterespondstoselector:@selector(passname:)]) {
[_delegatepassname: text];
}
return Basic;
}
if (TextField = = Cardfield) {
// allow only English and digital input
Nscharacterset *cs;
cs = [[nscharactersetcharactersetwithcharactersinstring:kenglishnum]invertedset];
nsstring *filtered = [[StringComponentsseparatedbycharactersinset: CS] Co Mponentsjoinedbystring:@ ""];
BOOL basic = [string isequaltostring: filtered];
nsstring *text = [TextField. Textstringbyreplacingcharactersinrange: Range withstring: string];
if (_delegate && [_delegaterespondstoselector:@selector(passcard:)]) {
[_delegatepasscard: text];
}
return Basic;
}
return YES;
}
ios-How to kill Uitextfield's long-press gesture menu and input content restrictions