TextProcessing
public protocol TextProcessing
An object capable of intercepting and modifying the text and attributes in an EditorView when registered with the EditorView.
-
Name of the TextProcessor
Declaration
Swift
var name: String { get } -
Priority of the TextProcessor. An
EditorViewcan have multipleTextProcessorsregistered. These will be executed in the order ofPriority.Attention
When atextProcessorwith priority is set to.exclusiveis executed, it will prevent all other registeredTextProcessorsfrom being executed. If multiple suchTextProcessorsare registered, only the one registered earlier is executed. When this happens, all other registeredTextProcessorswhich are prevented from being executed receiveprocessInterruptedwith therangein which exclusiveTextProcessoris executed. It is responsibility of theseTextProcessorsto do any cleanup/rollback if that needs to be done.Declaration
Swift
var priority: TextProcessingPriority { get } -
shouldProcess(_:Default implementationshouldProcessTextIn: replacementText: ) Determines if the text should be changed in the editor.
Note
This function is invoked just before making the changes in theEditorView. Besides preventing changing text in Editor in certain cases, this may also be used to do additional processing like updatingtypingAttributesonEditorViewbased on presence of certain attributes.Default Implementation
Declaration
Swift
func shouldProcess(_ editorView: EditorView, shouldProcessTextIn range: NSRange, replacementText text: String) -> BoolParameters
editorEditorViewin which text is being changed.rangeRange where new text is going to be added.
textText to be inserted.
-
Invoked before changes are processed by the editor.
Attention
This is fired before the text has changed in the editor. This can be helpful if any state needs to be changed based on edited text. However, it should be noted that the changes are done only inprocessand not in this function owing to the lifecycle of TextKit components.Declaration
Swift
func willProcess(editor: EditorView, deletedText: NSAttributedString, insertedText: NSAttributedString, range: NSRange)Parameters
editorEditorViewin which text is being changed.deletedTextText that has been deleted, if any.
insertedTextText that is inserted, if any.
rangeAffected range
-
Allows to change attributes and text in the
EditorViewas the text is changed.Declaration
Swift
func process(editor: EditorView, range editedRange: NSRange, changeInLength delta: Int) -> ProcessedParameters
editorEditorViewin which text is being changed.editedRangeCurrent range that is being modified.
deltaChange in length of the text as a result of typing text. The length may be more than 1 if multiple characters are selected before content is typed. It may also happen if text containing a string is pasted.
processedSet this to
trueis theTextProcessorhas made any changes to the text or attributes in theEditorViewReturn Value
Return
trueto indicate the processing had been done by the current processor. In case of .exclusive priority processors, returningtruenotifies all other processors of interruption. -
handleKeyWithModifiers(editor:Default implementationkey: modifierFlags: range: ) Allows to change attributes and text in the
EditorViewas the text is changed.Default Implementation
Declaration
Swift
func handleKeyWithModifiers(editor: EditorView, key: EditorKey, modifierFlags: UIKeyModifierFlags, range editedRange: NSRange)Parameters
editorEditorViewin which text is being changed.keyEditorKeythat is entered.modifierFlagsThe bit mask of modifier flags that were pressed with the key.
editedRangeCurrent range that is being modified.
-
Fired when processing has been interrupted by another
TextProcessorrunning in the same pass. This allowsTextProcessorto revert any changes that may not have been committed.Declaration
Swift
func processInterrupted(editor: EditorView, at range: NSRange)Parameters
editorEditorViewin which text is being changed.rangeCurrent range that is being modified.
-
selectedRangeChanged(editor:Default implementationoldRange: newRange: ) Notifies the processor that the selected range has changed in the EditorView due to a reason other than typing text for e.g. moving cursor using keyboard, mouse or tap.
Note
This function is also called if the user selects text by dragging. Each individual character selection during drag operation results in an independent call to this function. If old or new range has zero length, it indicates the caret (insertion point). If the range object is nil, it indicates that there is no previous/current selection.Default Implementation
Declaration
Swift
func selectedRangeChanged(editor: EditorView, oldRange: NSRange?, newRange: NSRange?)Parameters
editorEditorView in which selected range changed
oldRangeOriginal range before the change
newRangeCurrent range after the change
-
didProcess(editor:Default implementation) Invoked after the text has been processed in the
Editor.Default Implementation
Declaration
Swift
func didProcess(editor: EditorView)Parameters
editorEditorView in which text is changed.
View on GitHub
TextProcessing Protocol Reference