Peter 9bc996374f Add 'submodules/AsyncDisplayKit/' from commit '02bedc12816e251ad71777f9d2578329b6d2bef6'
git-subtree-dir: submodules/AsyncDisplayKit
git-subtree-mainline: d06f423e0ed3df1fed9bd10d79ee312a9179b632
git-subtree-split: 02bedc12816e251ad71777f9d2578329b6d2bef6
2019-06-11 18:42:43 +01:00

6.5 KiB
Executable File

title layout permalink prevPage nextPage
ASEditableTextNode docs /docs/editable-text-node.html scroll-node.html multiplex-image-node.html

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);

let editableTextNode = ASEditableTextNode()

editableTextNode.attributedText = NSAttributedString(string: "Lorem ipsum dolor sit amet.")
editableTextNode.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 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..."];
editableTextNode.attributedPlaceholderText = NSAttributedString(string: "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]};
editableTextNode.typingAttributes = [NSForegroundColorAttributeName: UIColor.blue, 
                                      NSBackgroundColorAttributeName: UIColor.red]

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;
optional public func editableTextNodeDidBeginEditing(_ editableTextNode: ASEditableTextNode)

-- 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;
optional public func editableTextNode(_ editableTextNode: ASEditableTextNode, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool

-- 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;
optional public func editableTextNodeDidChangeSelection(_ editableTextNode: ASEditableTextNode, fromSelectedRange: NSRange, toSelectedRange: NSRange, dueToEditing: Bool)

-- Indicates to the delegate that the text node's text was updated.

SwiftObjective-C
- (void)editableTextNodeDidUpdateText:(ASEditableTextNode *)editableTextNode;
optional public func editableTextNodeDidUpdateText(_ editableTextNode: ASEditableTextNode)

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

SwiftObjective-C
- (void)editableTextNodeDidFinishEditing:(ASEditableTextNode *)editableTextNode;
optional public func editableTextNodeDidFinishEditing(_ editableTextNode: ASEditableTextNode)