在 UITextField swift 3.0 中使用数字键盘以 dd-MM-yyyy 格式输入日期

问题描述 投票:0回答:3

我想在 UITextField 中输入时格式化(dd-MM-yyyy)文本,我使用的是 swift 3.0,有什么建议我如何实现相同的功能。

ios objective-c iphone swift swift3
3个回答
16
投票

一样使用
// create one textfield
@IBOutlet var txtDOB: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // set delegate for your textfield
    txtDOB.delegate = self

}

// 在

textfield
委托中调用您的函数
shouldChangeCharactersIn

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //Format Date of Birth dd-MM-yyyy

    //initially identify your textfield

    if textField == txtDOB {

        // check the chars length dd -->2 at the same time calculate the dd-MM --> 5
        if (txtDOB?.text?.characters.count == 2) || (txtDOB?.text?.characters.count == 5) {
            //Handle backspace being pressed
            if !(string == "") {
                // append the text
                txtDOB?.text = (txtDOB?.text)! + "-"
            }
        }
        // check the condition not exceed 9 chars
        return !(textField.text!.characters.count > 9 && (string.characters.count ) > range.length)
    }
    else {
        return true
    }
}

目标C

  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {
//Format Date of Birth dd-MM-yyyy

 if(textField == txtDOB)
    {
    if ((txtDOB.text.length == 2)||(txtDOB.text.length == 5))
        //Handle backspace being pressed
        if (![string isEqualToString:@""])
            txtDOB.text = [txtDOB.text stringByAppendingString:@"-"];
    return !([textField.text length]>9 && [string length] > range.length);
}
else
    return YES;

}

4
投票

针对 Swift 5 进行了更新

if textField == dateTextField {
            if dateTextField.text?.count == 2 || dateTextField.text?.count == 5 {
                //Handle backspace being pressed
                if !(string == "") {
                    // append the text
                    dateTextField.text = dateTextField.text! + "."
                }
            }
            // check the condition not exceed 9 chars
            return !(textField.text!.count > 9 && (string.count ) > range.length)
        } else {
            return true
        }

0
投票

这是一个 SwiftUI 解决方案。

struct DobScreen: View {
@State private var dobText: String = ""
@State var textLen = 0

var body: some View {
        VStack(alignment: .leading) {
            TextField("MM-DD-YYYY", text: $dobText)
            .onChange(of: dobText) { newValue in
                
                if newValue.count < textLen {
                    // If the length of the new value is less than the previous length,
                    // it indicates a deletion (backspace).
                    dobText = ""
                    textLen = 0
                    return
                }
                // Check if the input length exceeds 10 characters
                 guard newValue.count <= 10 else {
                     dobText = String(newValue.prefix(10))
                     return
                 }
                
                 // Handle backspace
                 if newValue.count < dobText.count {
                     // If the length of the new value is less than the previous length,
                     // it indicates a deletion (backspace).
                     dobText = ""
                     return
                 }
                 
                 // Add separators for MM / DD / YYYY format
                 if newValue.count >= 2 && !newValue.contains("-") {
                     dobText.insert("-", at: dobText.index(dobText.startIndex, offsetBy: 2))
                 }
                 if newValue.count >= 5 && !newValue[dobText.index(dobText.startIndex, offsetBy: 5)...].contains("-") {
                     dobText.insert("-", at: dobText.index(dobText.startIndex, offsetBy: 5))
                 }
                textLen = dobText.count
            }
        }.padding()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.