Notice
Recent Posts
Recent Comments
Link
아미(아름다운미소)
UITextField 또는 UITextView 문자 수를 제한하는 방법 본문
UITextField또는 UITextView에 사용자가 특정 글자 수보다 더를 입력되는것을 방지하려면 shouldChangeCharactersIn(텍스트 필드) 또는 shouldChangeTextIn(텍스트 뷰) 를 사용하시면 됩니다.
UITextField (한 줄)로 작업하는지 또는 UITextView (여러 줄)로 작업하는지에 따라서 두 가지 방법 중 하나를 사용 하시면 됩니다.
- textField
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let currentText = textField.text ?? "" guard let stringRange = Range(range, in: currentText) else { return false } let updatedText = currentText.replacingCharacters(in: stringRange, with: string) return updatedText.count <= 16 }
- textView
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { let currentText = textView.text ?? "" guard let stringRange = Range(range, in: currentText) else { return false } let changedText = currentText.replacingCharacters(in: stringRange, with: text) return changedText.count <= 16 }
'랭귀지 > SWIFT' 카테고리의 다른 글
Change Background Color Status Bar iOS (0) | 2018.05.22 |
---|---|
UITableViewCell 선택 스타일을 사용하지 않도록 설정 (0) | 2018.05.21 |
이미지가 더 작은 경우에도 UITableViewCell의 ImageView를 고정 크기로 만드는 방법 (0) | 2018.05.18 |
swift UIColor에 RGBA 값 적용하기 (0) | 2018.05.17 |
리눅스에서 swap 메모리 초기화 (0) | 2018.05.16 |
Comments