回覆列表
  • 1 # 使用者9538664110570

    首先要重寫UITextFiled子類在初始化方法下面copy下面方法就禁止了長按出現的選單//幹掉textField的長按選單禁止copypaste-(BOOL)canPerformAction:(SEL)actionwithSender:(id)sender{if([UIMenuControllersharedMenuController]){[UIMenuControllersharedMenuController].menuVisible=NO;}returnNO;}在開發中我們有時候會對UITextFiled進行一些操作,說一下如何只輸入中文,或者只輸入數字和英文2中方法我直接用宏定義定義:#definekAlphaNum@"abcdefghijklmnopqrstuvwxyz"//這種是對應只輸入中文不允許其他數字或者符號的#definekEnglishNum@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"//這種是對應只能輸入數字和英文的方法執行UITextField的delegate方法切記textFiled.delegate=self;下面直接上程式碼:-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{UITextField*nameField=(UITextField*)[selfviewWithTag:200];UITextField*cardField=(UITextField*)[selfviewWithTag:201];if(textField==nameField){//只允許輸入中文NSCharacterSet*cs;cs=[[NSCharacterSetcharacterSetWithCharactersInString:kAlphaNum]invertedSet];NSString*filtered=[[stringcomponentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];BOOLbasic=[stringisEqualToString:filtered];NSString*text=[textField.textstringByReplacingCharactersInRange:rangewithString:string];if(_delegate&&[_delegaterespondsToSelector:@selector(passName:)]){[_delegatepassName:text];}returnbasic;}if(textField==cardField){//只允許輸入英文和數字NSCharacterSet*cs;cs=[[NSCharacterSetcharacterSetWithCharactersInString:kEnglishNum]invertedSet];NSString*filtered=[[stringcomponentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];BOOLbasic=[stringisEqualToString:filtered];NSString*text=[textField.textstringByReplacingCharactersInRange:rangewithString:string];if(_delegate&&[_delegaterespondsToSelector:@selector(passCard:)]){[_delegatepassCard:text];}returnbasic;}returnYES;}

  • 中秋節和大豐收的關聯?
  • exo新歌playboy中文版歌詞分配?