Skip to content

Commit

Permalink
feat: 텍스트 필드의 글자수 변화에 따른 전화번호 구현 (TeamNADA#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyun99999 committed Apr 15, 2023
1 parent 30c13aa commit 05762a7
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class FrontCardCreationCollectionViewCell: UICollectionViewCell {
registerCell()
textFieldDelegate()
setNotification()
setAddTargets()
}
}

Expand Down Expand Up @@ -223,6 +224,9 @@ extension FrontCardCreationCollectionViewCell {
defaultImageIndex: defaultImageIndex))
}
}
private func setAddTargets() {
phoneNumberTextField.addTarget(self, action: #selector(phoneNumberTextFieldDidChange), for: .editingChanged)
}
static func nib() -> UINib {
return UINib(nibName: Const.Xib.frontCardCreationCollectionViewCell, bundle: Bundle(for: FrontCardCreationCollectionViewCell.self))
}
Expand Down Expand Up @@ -327,6 +331,22 @@ extension FrontCardCreationCollectionViewCell {
backgroundSettingCollectionView.deselectItem(at: IndexPath.init(item: 0, section: 0), animated: true)
}
}
@objc
private func phoneNumberTextFieldDidChange() {
if let text = phoneNumberTextField.text?.replacingOccurrences(of: "-", with: "") {
let textArray = text.map { String($0) }

if text.count < 10 {
phoneNumberTextField.text = text
} else if text.count == 10 {
phoneNumberTextField.text = textArray[0...2].joined() + "-" + textArray[3...5].joined() + "-" + textArray[6...9].joined()
} else if text.count == 11 {
phoneNumberTextField.text = textArray[0...2].joined() + "-" + textArray[3...6].joined() + "-" + textArray[7...10].joined()
} else {
phoneNumberTextField.text = text
}
}
}
}

// MARK: - UICollectionViewDelegate
Expand Down

0 comments on commit 05762a7

Please sign in to comment.