Contents

shouldChangeText(in:replacementString:)

Initiates a series of delegate messages (and general notifications) to determine whether modifications can be made to the characters and attributes of the receiver’s text.

Declaration

func shouldChangeText(in affectedCharRange: NSRange, replacementString: String?) -> Bool

Parameters

  • affectedCharRange:

    The range of characters affected by the proposed change.

  • replacementString:

    The characters that will replace those in affectedCharRange. If only text attributes are being changed, replacementString is nil.

Return Value

true to allow the change, false to prohibit it.

Discussion

This method checks with the delegate as needed using textShouldBeginEditing(_:) and textView(_:shouldChangeTextIn:replacementString:).

This method must be invoked at the start of any sequence of user-initiated editing changes. If your subclass of NSTextView implements methods that modify the text, make sure to invoke this method to determine whether the change should be made. If the change is allowed, complete the change by invoking the didChangeText() method.

Special Considerations

If you override this method, you must call super at the beginning of the override.

If the receiver is not editable, this method automatically returns false. This result prevents instances in which a text view could be changed by user actions even though it had been set to be non-editable.

In macOS 10.4 and later, if there are multiple selections, this method acts on the first selected subrange.

See Also

Related Documentation

Customizing subclass behaviors