<Instance method> textField(_:shouldChangeCharactersIn:replacementString:)

dlsl7380·2021년 6월 13일
0

Instance method

목록 보기
7/19

textField(_:shouldChangeCharactersIn:replacementString:)

Asks the delegate whether to change the specified text.
Availability

Declaration

optional func textField(_ textField: UITextField, 
shouldChangeCharactersIn range: NSRange, 
      replacementString string: String) -> Bool

Parameters

textField

The text field containing the text.

range

The range of characters to be replaced.

string

The replacement string for the specified range. During typing, this parameter normally contains only the single new character that was typed, but it may contain more characters if the user is pasting text. When the user deletes one or more characters, the replacement string is empty.

Return Value

true if the specified text range should be replaced; otherwise, false to keep the old text.

Discussion

The text field calls this method whenever user actions cause its text to change. Use this method to validate text as it is typed by the user. For example, you could use this method to prevent the user from entering anything but numerical values.


위의 사진에서 { 커서의 오른쪽 index, 원래 문자열 길이에서 생긴 변화에대한 길이}임. 새롭게 문자를 입력했을 때는 0이기때문에
입력할경우 좌측에서 문자수만큼 플러스되는것이고
지울경우 우측값만큼 원래있던 문자열 인덱스에서 빼준다. 그리고 그 값이 좌측값이 된다.

결국 좌측값은 현재 최종적으로 있는 커서의 index이고
우측값은 원래 있던 문자열textField에서 좌측으로 이동한 값이다.

0개의 댓글

관련 채용 정보