textfield
の最大文字数は16で、4文字ごとにマイナス文字またはスペースを追加してからこのサンプルのような残りの文字を入力したい5022-2222- 2222-2222。 私のコードはありますが、それはうまくいかず、どうすればいいですか?テキスト入力時のテキストフィールド文字間のスペースの追加
if textField.text?.characters.count == 5 {
let l = textField.text?.characters.count
let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
cartNumberTextField.attributedText = attributedString
}
else if textField.text?.characters.count == 9 {
let l = textField.text?.characters.count
let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
cartNumberTextField.attributedText = attributedString
}
else if textField.text?.characters.count == 13 {
let l = textField.text?.characters.count
let attributedString = NSMutableAttributedString(string: cartNumberTextField.text!)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(4.0), range: NSRange(location: l!, length: 4))
cartNumberTextField.attributedText = attributedString
}
私はUITextField
shouldChangeCharactersIn
範囲メソッドでこのコードを追加しています。