Developer/iOS

UITextView에서 return 눌렀을때, 이벤트 받기.

블로blow 2010. 11. 13. 16:41
728x90
//이 메소드는 원래 textview에 text의 change가 있을때, 실행되는 delegate입니다.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text
{
    //textView에 어느 글을 쓰더라도 이 메소드를 호출합니다.
    if ([text isEqualToString:@"\n"]) {
        // return키를 누루면 원래 줄바꿈이 일어나므로 \n을 입력하는데 \n을 입력하면 실행하게 합니다.
        [textView resignFirstResponder]; //키보드를 닫는 메소드입니다.
        return FALSE; //리턴값이 FALSE이면, 입력한 값이 입력되지 않습니다.
    }
    return TRUE; //평소에 경우에는 입력을 해줘야 하므로, TRUE를 리턴하면 TEXT가 입력됩니다.
}

Document소개.

Asks the delegate whether the specified text should be replaced in the text view.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

Parameters

textView
The text view containing the changes.
range
The current selection range. If the length of the range is 0, range reflects the current insertion point. If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character.
text
The text to insert.

Return Value
YES if the old text should be replaced by the new text; NO if the replacement operation should be aborted.

Discussion
The text view calls this method whenever the user types a new character or deletes an existing character. Implementation of this method is optional. You can use this method to replace text before it is committed to the text view storage. For example, a spell checker might use this method to replace a misspelled word with the correct spelling.


728x90