iOS Developer Library

Developer

UIKit Framework Reference UITextViewDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

UITextViewDelegate

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later.

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.

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

    Declaration

    Swift

    optional func textViewShouldBeginEditing(_ textView: UITextView) -> Bool

    Objective-C

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView

    Parameters

    textView

    The text view for which editing is about to begin.

    Return Value

    YEStrue if an editing session should be initiated; otherwise, NOfalse 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 YEStrue 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 YEStrue.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textViewDidBeginEditing(_ textView: UITextView)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textViewShouldEndEditing(_ textView: UITextView) -> Bool

    Objective-C

    - (BOOL)textViewShouldEndEditing:(UITextView *)textView

    Parameters

    textView

    The text view for which editing is about to end.

    Return Value

    YEStrue if editing should stop; otherwise, NOfalse 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 YEStrue from this method to allow the text view to resign the first responder status. You might return NOfalse, however, in cases where your delegate wants to validate the contents of the text view. By returning NOfalse, 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 NOfalse 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 YEStrue.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textViewDidEndEditing(_ textView: UITextView)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textView(_ textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool

    Objective-C

    - (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

    YEStrue if the old text should be replaced by the new text; NOfalse 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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textViewDidChange(_ textView: UITextView)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textViewDidChangeSelection(_ textView: UITextView)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    optional func textView(_ textView: UITextView, shouldInteractWithTextAttachment textAttachment: NSTextAttachment, inRange characterRange: NSRange) -> Bool

    Objective-C

    - (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

    YEStrue if interaction with the text attachment should be allowed; NOfalse 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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    optional func textView(_ textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool

    Objective-C

    - (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

    YEStrue if interaction with the URL should be allowed; NOfalse 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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.