2016-07-26 2 views
1

그래서 UITextField은 숫자 만 허용하고 맞춤 키보드 입력을 사용하여 해결했습니다.신속하게 UITextField를 mm-dd-yy 형식으로 설정하십시오.

UITextField의 의도는 누군가의 생일을 얻는 것입니다. 외모가 맘에 들지 않기 때문에 UIDatePicker을 사용하고 싶지 않습니다.

TextField은 사용자가 TextField에 넣는 두 번째 자릿수마다 자동으로 대시를 삽입합니다.

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. 는 텍스트 필드에 대한 대표와 태그를 추가합니다. '-'가 자동으로 추가됩니다
  3. 그런 다음 사용자에 대해 걱정할 필요가 자신의 DOB.No를 입력 할 수있는 지금의이 다음 대리자 메서드

    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 
} 
관련 문제