UITextViewDelegate Protocol Reference

Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 2.0 and later.
Declared in
UITextView.h
Related sample code

Overview

The UITextViewDelegate protocol defines a set of optional methods you can use to receive editing-related messages for UITextView objects. All of the methods in this protocol are optional. You can use them in situations where you might want to adjust the text being edited (such as in the case of a spell checker program) or modify the intended insertion point.

Tasks

Responding to Editing Notifications

Responding to Text Changes

Responding to Selection Changes

Interacting with Text Data

Instance Methods

textView:shouldChangeTextInRange:replacementText:

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.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textView:shouldInteractWithTextAttachment:inRange:

Asks the delegate if the specified text view should allow user interaction with the provided text attachment in the given range of text.

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
Parameters
textView

The text view containing the text attachment.

textAttachment

The text attachment.

characterRange

The character range containing the text attachment.

Return Value

YES if interaction with the text attachment should be allowed; NO if interaction should not be allowed.

Discussion

The text view calls this method if the user taps or long-presses the text attachment and its image property is not nil. Implementation of this method is optional. You can use this method to trigger an action in addition to displaying the text attachment inline with the text.

Availability
  • Available in iOS 7.0 and later.
Declared In
UITextView.h

textView:shouldInteractWithURL:inRange:

Asks the delegate if the specified text view should allow user interaction with the given URL in the given range of text.

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
Parameters
textView

The text view containing the text attachment.

URL

The URL to be processed.

characterRange

The character range containing the URL.

Return Value

YES if interaction with the URL should be allowed; NO if interaction should not be allowed.

Discussion

The text view calls this method if the user taps or long-presses the URL link. Implementation of this method is optional. By default, the text view opens the application responsible for handling the URL type and passes it the URL. You can use this method to trigger an alternative action, such as displaying the web content at the URL in a web view within the current application.

Availability
  • Available in iOS 7.0 and later.
Declared In
UITextView.h

textViewDidBeginEditing:

Tells the delegate that editing of the specified text view has begun.

- (void)textViewDidBeginEditing:(UITextView *)textView
Parameters
textView

The text view in which editing began.

Discussion

Implementation of this method is optional. A text view sends this message to its delegate immediately after the user initiates editing in a text view and before any changes are actually made. You can use this method to set up any editing-related data structures and generally prepare your delegate to receive future editing messages.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textViewDidChange:

Tells the delegate that the text or attributes in the specified text view were changed by the user.

- (void)textViewDidChange:(UITextView *)textView
Parameters
textView

The text view containing the changes.

Discussion

The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.

Implementation of this method is optional.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textViewDidChangeSelection:

Tells the delegate that the text selection changed in the specified text view.

- (void)textViewDidChangeSelection:(UITextView *)textView
Parameters
textView

The text view whose selection changed.

Discussion

Implementation of this method is optional. You can use the selectedRange property of the text view to get the new selection.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textViewDidEndEditing:

Tells the delegate that editing of the specified text view has ended.

- (void)textViewDidEndEditing:(UITextView *)textView
Parameters
textView

The text view in which editing ended.

Discussion

Implementation of this method is optional. A text view sends this message to its delegate after it closes out any pending edits and resigns its first responder status. You can use this method to tear down any data structures or change any state information that you set when editing began.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textViewShouldBeginEditing:

Asks the delegate if editing should begin in the specified text view.

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
Parameters
textView

The text view for which editing is about to begin.

Return Value

YES if an editing session should be initiated; otherwise, NO to disallow editing.

Discussion

When the user performs an action that would normally initiate an editing session, the text view calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed.

Implementation of this method by the delegate is optional. If it is not present, editing proceeds as if this method had returned YES.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h

textViewShouldEndEditing:

Asks the delegate if editing should stop in the specified text view.

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
Parameters
textView

The text view for which editing is about to end.

Return Value

YES if editing should stop; otherwise, NO if the editing session should continue

Discussion

This method is called when the text view is asked to resign the first responder status. This might occur when the user tries to change the editing focus to another control. Before the focus actually changes, however, the text view calls this method to give your delegate a chance to decide whether it should.

Normally, you would return YES from this method to allow the text view to resign the first responder status. You might return NO, however, in cases where your delegate wants to validate the contents of the text view. By returning NO, you could prevent the user from switching to another control until the text view contained a valid value.

Be aware that this method provides only a recommendation about whether editing should end. Even if you return NO from this method, it is possible that editing might still end. For example, this might happen when the text view is forced to resign the first responder status by being removed from its parent view or window.

Implementation of this method by the delegate is optional. If it is not present, the first responder status is resigned as if this method had returned YES.

Availability
  • Available in iOS 2.0 and later.
Declared In
UITextView.h