2016-07-26 7 views
1

UITextFieldには数字だけを入力し、カスタムキーボード入力を使用して解決します。即時にUITextFieldをmm-dd-yy形式に設定してください

このUITextFieldの目的は誰か誕生日を取得することです。私は外観が気に入らないので、UIDatePickerを使いたくないです。

TextFieldは、ユーザーがTextFieldに入れた2桁目ごとに自動的にダッシュを挿入したいと考えています。

dd-mm-yyはプレースホルダテキストです。私はダッシュを永久に作ることを考えていましたが、それをどうやって行うかわかりません。

どうすればいいですか?

+0

あなたのアイデアhttp://stackoverflow.com/questions/34454532/how-add-separator-to-string-at-every-を得ればこれを見ますn-characters-in-swift –

+0

'shouldChangeCharactersInRange'を使用して、追加された新しいテキスト/文字を確認し、必要に応じてcount(text.length)をチェックしてハイフン記号を追加します。参照:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString: – Mrunal

答えて

1

このテキストボックスにテキストを入力できるようにしますdd-mm-yy右ですか? これは私がこれがあなたを助けると確信しているならば。

  1. クラスの先頭に、この変数を宣言します。この変数は後で使用します。

    var dateFormate = Bool() 
    
  2. そのTextFieldにデリゲートとタグを追加します。それが今、ユーザーが自分のDOB.Noを入力することができます

  3. その後

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
    
    //1. To make sure that this is applicable to only particular textfield add tag. 
    if textField.tag == 1 { 
        //2. this one helps to make sure that user enters only numeric characters and '-' in fields 
        let numbersOnly = NSCharacterSet(charactersInString: "1234567890-") 
        let characterSetFromTextField = NSCharacterSet(charactersInString: string) 
    
        let Validate:Bool = numbersOnly .isSupersetOfSet(characterSetFromTextField) 
        if !Validate { 
         return false; 
        } 
        if range.length + range.location > textField.text?.characters.count { 
         return false 
        } 
        let newLength = (textField.text?.characters.count)! + string.characters.count - range.length 
        if newLength == 3 || newLength == 6 { 
         let char = string.cStringUsingEncoding(NSUTF8StringEncoding)! 
         let isBackSpace = strcmp(char, "\\b") 
    
         if (isBackSpace == -92) { 
          dateFormate = false; 
         }else{ 
          dateFormate = true; 
         } 
    
         if dateFormate { 
          let textContent:String! 
          textContent = textField.text 
          //3.Here we add '-' on overself. 
          let textWithHifen:NSString = "\(textContent)-" 
          textField.text = textWithHifen as String 
          dateFormate = false 
         } 
        } 
        //4. this one helps to make sure only 8 character is added in textfield .(ie: dd-mm-yy) 
        return newLength <= 8; 
    
    } 
    return true 
    } 
    
  4. この次デリゲートメソッドを追加するには、心配する必要は「 - 」それは自動的に追加されます。

スウィフト3:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 

    //1. To make sure that this is applicable to only particular textfield add tag. 
    if textField.tag == 1 { 
     //2. this one helps to make sure that user enters only numeric characters and '-' in fields 
     let numbersOnly = CharacterSet(charactersIn: "1234567890-") 

     let Validate = string.rangeOfCharacter(from: numbersOnly.inverted) == nil ? true : false 
     if !Validate { 
      return false; 
     } 
     if range.length + range.location > textField.text?.characters.count { 
      return false 
     } 
     let newLength = (textField.text?.characters.count)! + string.characters.count - range.length 
     if newLength == 3 || newLength == 6 { 
      let char = string.cString(using: NSUTF8StringEncoding)! 
      let isBackSpace = strcmp(char, "\\b") 

      if (isBackSpace == -92) { 
       dateFormate = false; 
      }else{ 
       dateFormate = true; 
      } 

      if dateFormate { 
       let textContent:String! 
       textContent = textField.text 
       //3.Here we add '-' on overself. 
       let textWithHifen:NSString = "\(textContent)-" 
       textField.text = textWithHifen as String 
       dateFormate = false 
      } 
     } 
     //4. this one helps to make sure only 8 character is added in textfield .(ie: dd-mm-yy) 
     return newLength <= 8; 

    } 
    return true 
} 
関連する問題