AsyncDisplayKit is now Texture! LEARN MORE

Texture

ASEditableTextNode

ASEditableTextNode is available to be used anywhere you’d normally use a UITextView or UITextField. Under the hood, it uses a specialized UITextView as its backing view. You can access and configure this view directly any time after the node has loaded, as long as you do it on the main thread.

It’s also important to note that this node does not support layer backing due to the fact that it supports user interaction.

Basic Usage

Using an editable text node as a text input is easy. If you want it to have text by default, you can assign an attributed string to the attributedText property.

SwiftObjective-C
ASEditableTextNode *editableTextNode = [[ASEditableTextNode alloc] init];

editableTextNode.attributedText = [[NSAttributedString alloc] initWithString:@"Lorem ipsum dolor sit amet."];
editableTextNode.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);

Placeholder Text

If you want to display a text box with a placeholder that disappears after a user starts typing, just assign an attributed string to the attributedPlaceholderText property.

SwiftObjective-C
editableTextNode.attributedPlaceholderText = [[NSAttributedString alloc] initWithString:@"Type something here..."];

The property isDisplayingPlaceholder will initially return YES, but will toggle to NO any time the attributedText property is set to a non-empty string.

Typing Attributes

To set up the style of the text your user will type into this text field, you can set the typingAttributes.

SwiftObjective-C
editableTextNode.typingAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor], 
                                      NSBackgroundColorAttributeName: [UIColor redColor]};

ASEditableTextNode Delegate

In order to respond to events associated with an editable text node, you can use any of the following delegate methods:

– Indicates to the delegate that the text node began editing.

SwiftObjective-C
- (void)editableTextNodeDidBeginEditing:(ASEditableTextNode *)editableTextNode;

– Asks the delegate whether the specified text should be replaced in the editable text node.

SwiftObjective-C
- (BOOL)editableTextNode:(ASEditableTextNode *)editableTextNode shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

– Indicates to the delegate that the text node’s selection has changed.

SwiftObjective-C
- (void)editableTextNodeDidChangeSelection:(ASEditableTextNode *)editableTextNode fromSelectedRange:(NSRange)fromSelectedRange toSelectedRange:(NSRange)toSelectedRange dueToEditing:(BOOL)dueToEditing;

– Indicates to the delegate that the text node’s text was updated.

SwiftObjective-C
- (void)editableTextNodeDidUpdateText:(ASEditableTextNode *)editableTextNode;

–  Indicates to the delegate that the text node has finished editing.

SwiftObjective-C
- (void)editableTextNodeDidFinishEditing:(ASEditableTextNode *)editableTextNode;

Edit on GitHub