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
EditorView
can have multipleTextProcessors
registered. These will be executed in the order ofPriority
.Attention
When atextProcessor
with priority is set to.exclusive
is executed, it will prevent all other registeredTextProcessors
from being executed. If multiple suchTextProcessors
are registered, only the one registered earlier is executed. When this happens, all other registeredTextProcessors
which are prevented from being executed receiveprocessInterrupted
with therange
in which exclusiveTextProcessor
is executed. It is responsibility of theseTextProcessors
to 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 updatingtypingAttributes
onEditorView
based on presence of certain attributes.Default Implementation
Declaration
Swift
func shouldProcess(_ editorView: EditorView, shouldProcessTextIn range: NSRange, replacementText text: String) -> Bool
Parameters
editor
EditorView
in which text is being changed.range
Range where new text is going to be added.
text
Text 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 inprocess
and not in this function owing to the lifecycle of TextKit components.Declaration
Swift
func willProcess(editor: EditorView, deletedText: NSAttributedString, insertedText: NSAttributedString, range: NSRange)
Parameters
editor
EditorView
in which text is being changed.deletedText
Text that has been deleted, if any.
insertedText
Text that is inserted, if any.
range
Affected range
-
Allows to change attributes and text in the
EditorView
as the text is changed.Declaration
Swift
func process(editor: EditorView, range editedRange: NSRange, changeInLength delta: Int) -> Processed
Parameters
editor
EditorView
in which text is being changed.editedRange
Current range that is being modified.
delta
Change 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.
processed
Set this to
true
is theTextProcessor
has made any changes to the text or attributes in theEditorView
Return Value
Return
true
to indicate the processing had been done by the current processor. In case of .exclusive priority processors, returningtrue
notifies all other processors of interruption. -
handleKeyWithModifiers(editor:
Default implementationkey: modifierFlags: range: ) Allows to change attributes and text in the
EditorView
as the text is changed.Default Implementation
Declaration
Swift
func handleKeyWithModifiers(editor: EditorView, key: EditorKey, modifierFlags: UIKeyModifierFlags, range editedRange: NSRange)
Parameters
editor
EditorView
in which text is being changed.key
EditorKey
that is entered.modifierFlags
The bit mask of modifier flags that were pressed with the key.
editedRange
Current range that is being modified.
-
Fired when processing has been interrupted by another
TextProcessor
running in the same pass. This allowsTextProcessor
to revert any changes that may not have been committed.Declaration
Swift
func processInterrupted(editor: EditorView, at range: NSRange)
Parameters
editor
EditorView
in which text is being changed.range
Current 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
editor
EditorView in which selected range changed
oldRange
Original range before the change
newRange
Current 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
editor
EditorView in which text is changed.