Struct UIWritingToolsCoordinator

Source
#[repr(C)]
pub struct UIWritingToolsCoordinator { /* private fields */ }
Available on crate feature UIWritingToolsCoordinator only.
Expand description

An object that manages interactions between Writing Tools and your custom text view.

Add a UIWritingToolsCoordinator object to a custom view when you want to add Writing Tools support to that view. The coordinator manages interactions between your view and the Writing Tools UI and back-end capabilities. When creating a coordinator, you supply a delegate object to respond to requests from the system and provide needed information. Your delegate delivers your view’s text to Writing Tools, incorporates suggested changes back into your text storage, and supports the animations that Writing Tools creates to show the state of an operation.

Create the UIWritingToolsCoordinator object when setting up your UI, and initialize it with a custom object that adopts the UIWritingToolsCoordinator/Delegate protocol. Add the coordinator to your view using the UIView/addInteraction(_:) method. When a coordinator is present on a view, the system adds UI elements to initiate Writing Tools operations.

When defining the delegate, choose an object from your app that has access to your view and its text storage. You can adopt the UIWritingToolsCoordinator/Delegate protocol in the view itself, or in another type that your view uses to manage content. During the interactions with Writing Tools, the delegate gets and sets the contents of the view’s text storage and supports Writing Tools behaviors.

Note: You don’t need to create an UIWritingToolsCoordinator object if you display text using a UITextView, <doc ://com.apple.documentation/documentation/appkit/nstextview>, <doc ://com.apple.documentation/documentation/swiftui/text>, <doc ://com.apple.documentation/documentation/swiftui/textfield>, or <doc ://com.apple.documentation/documentation/swiftui/texteditor> view. Those views already include the required support to handle Writing Tools interactions.

See also Apple’s documentation

Implementations§

Source§

impl UIWritingToolsCoordinator

Source

pub unsafe fn isWritingToolsAvailable(mtm: MainThreadMarker) -> bool

A Boolean value that indicates whether Writing Tools features are currently available.

The value of this property is true when Writing Tools features are available, and false when they aren’t. Writing Tools support might be unavailable because of device constraints or because the system isn’t ready to process Writing Tools requests.

Source

pub unsafe fn initWithDelegate( this: Allocated<Self>, delegate: Option<&ProtocolObject<dyn UIWritingToolsCoordinatorDelegate>>, ) -> Retained<Self>

Creates a writing tools coordinator and assigns the specified delegate object to it.

  • Parameters:
  • delegate: An object capable of handling Writing Tools interactions for your view. The delegate must be able to modify your view’s text storage and refresh the view’s layout and appearance.

Create the coordinator object during your view’s initialization, and assign the object to your view. Use the UIView/addInteraction(_:) method to add the object to your view.

Source

pub unsafe fn delegate( &self, ) -> Option<Retained<ProtocolObject<dyn UIWritingToolsCoordinatorDelegate>>>

The object that handles Writing Tools interactions for your view.

Specify this object at initialization time when creating your UIWritingToolsCoordinator object. The object must adopt the UIWritingToolsCoordinator/Delegate protocol, and be capable of modifying your view’s text storage and refreshing the view’s layout and appearance.

Source

pub unsafe fn effectContainerView(&self) -> Option<Retained<UIView>>

Available on crate features UIResponder and UIView only.

The view that Writing Tools uses to display visual effects during the text-rewriting process.

Writing Tools uses the view in this property to host the visual effects it creates when making interactive changes to your view’s content. These visual effects let people know the state of the text and provide feedback about what’s happening to it. Set this property to a subview that sits visually above, and covers, all of the text in your custom text view. If you don’t assign a value to this property, the coordinator uses the object in its UIInteraction/view property to host any visual effects.

If you display your view’s text using multiple text containers, implement the UIWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:requestsSingleContainerSubrangesOf:in:completion:) method to request multiple previews.

Source

pub unsafe fn setEffectContainerView( &self, effect_container_view: Option<&UIView>, )

Available on crate features UIResponder and UIView only.

This is a weak property. Setter for effectContainerView.

Source

pub unsafe fn decorationContainerView(&self) -> Option<Retained<UIView>>

Available on crate features UIResponder and UIView only.

The view that Writing Tools uses to display background decorations such as proofreading marks.

Writing Tools uses the view in this property to host proofreading marks and other visual elements that show any suggested changes. Set this property to a subview situated visibly below the text in your custom text view. It’s also satisfactory to place this view visually in front of the text. Make sure the size of the view is big enough to cover all of the affected text. If you don’t assign a value to this property, the coordinator uses the object in its UIInteraction/view property to host any visual elements.

If you display your view’s text using multiple text containers, implement the UIWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:requestsSingleContainerSubrangesOf:in:completion:) and UIWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:requestsDecorationContainerViewFor:in:completion:) methods to provide separate decoration views for each container.

Source

pub unsafe fn setDecorationContainerView( &self, decoration_container_view: Option<&UIView>, )

Available on crate features UIResponder and UIView only.

This is a weak property. Setter for decorationContainerView.

Source

pub unsafe fn state(&self) -> UIWritingToolsCoordinatorState

The current level of Writing Tools activity in your view.

Use this property to determine when Writing Tools is actively making changes to your view. During the course of Writing Tools interactions, the system reports state changes to the delegate’s UIWritingToolsCoordinator/Delegate/writingToolsCoordinator(_:willChangeTo:completion:) method and updates this property accordingly.

Source

pub unsafe fn stopWritingTools(&self)

Stops the current Writing Tools operation and dismisses the system UI.

Call this method to abort the current Writing Tools operation. This method dismisses the system’s Writing Tools UI and stops any in-flight interactions with your view. This method does not undo any changes that Writing Tools already made to your view’s content.

Source

pub unsafe fn preferredBehavior(&self) -> UIWritingToolsBehavior

Available on crate feature UITextInputTraits only.

The level of Writing Tools support you want the system to provide for your view.

Use this property to request an inline or panel-based experience, or to disable Writing Tools for your view altogether. The default value of this property is UIWritingToolsBehavior/default.

Source

pub unsafe fn setPreferredBehavior( &self, preferred_behavior: UIWritingToolsBehavior, )

Available on crate feature UITextInputTraits only.

Setter for preferredBehavior.

Source

pub unsafe fn behavior(&self) -> UIWritingToolsBehavior

Available on crate feature UITextInputTraits only.

The actual level of Writing Tools support the system provides for your view.

The system chooses this value based on the device capabilities, and takes the value in the preferredBehavior property into consideration when making the choice. The value in this property is never the default option, and is instead one of the specific options such as UIWritingToolsBehavior/none, UIWritingToolsBehavior/limited, or UIWritingToolsBehavior/complete.

Source

pub unsafe fn preferredResultOptions(&self) -> UIWritingToolsResultOptions

Available on crate feature UITextInputTraits only.

The type of content you allow Writing Tools to generate for your custom text view.

Writing Tools can create plain text or rich text, and it can format text using lists or tables as needed. If your view doesn’t support specific types of content, specify the types you do support in this property. The default value of this property is UIWritingToolsResultOptions/default, which lets the system determine the type of content to generate.

Source

pub unsafe fn setPreferredResultOptions( &self, preferred_result_options: UIWritingToolsResultOptions, )

Available on crate feature UITextInputTraits only.
Source

pub unsafe fn resultOptions(&self) -> UIWritingToolsResultOptions

Available on crate feature UITextInputTraits only.

The type of content the system generates for your custom text view.

This property contains the set of options that Writing Tools outputs for your view. Writing Tools takes the value in the UIWritingToolsCoordinator/preferredResultOptions property into consideration when determining this value.

Source

pub unsafe fn updateRange_withText_reason_forContextWithIdentifier( &self, range: NSRange, replacement_text: &NSAttributedString, reason: UIWritingToolsCoordinatorTextUpdateReason, context_id: &NSUUID, )

Informs the coordinator about changes your app made to the text in the specified context object.

  • Parameters:
  • range: The range of text to replace. This range is relative to the starting location of the specified context object’s text in your view’s text storage. If you initialized the context object with the entire contents of your view’s text storage, specify the range of text you’re replacing in your text storage. However, if you initialized the context object with only a portion of your view’s text, specify a range that is relative to the starting location of the context object’s text.
  • replacementText: The text that replaces the previous content in range. Specify an empty string to delete the text in the specified range.
  • reason: The reason you updated the text.
  • contextID: The unique identifier of the context object that contains the text you modified.

If you make any changes to the text Writing Tools is evaluating, call this method to report those changes to your view’s coordinator object. You might make changes in response to an undo command or when someone types into the same part of your view’s text. Calling this method keeps the coordinator object informed of any changes, and ensures it delivers accurate information to its delegate. In response, the coordinator refreshes previews and other information related to your view. If the scope of the update is significantly large, the coordinator can optionally cancel the Writing Tools session altogether.

Use this method to report changes that precisely intersect your context object’s text. The first time you call this method for a context object, report changes only to the original attributed string in that object. If you call this method more than once, report changes to the newly modified version of that string. Don’t use this method to report changes to text that comes before or after the context object. If you make changes before your context object, report those changes separately using the UIWritingToolsCoordinator/updateForReflowedTextInContextWithIdentifier(_:) method.

Warning: Failure to call this method for a change can cause Writing Tools to deliver inaccurate information to your delegate and lead to data loss.

Source

pub unsafe fn updateForReflowedTextInContextWithIdentifier( &self, context_id: &NSUUID, )

Informs the coordinator that a change occurred to the view or its text that requires a layout update.

  • Parameters:
  • contextID: The unique identifier of the context object affected by the change. Pass the identifier for the context object that comes after the changes.

Use this method to inform Writing Tools when the geometry of your view changes, or when the text that precedes one of your context objects changes. Changes to the view’s geometry or text can affect the flow of any remaining text, and require a layout update. Writing Tools uses this method to refresh any layout-dependent information it’s currently tracking. For example, it uses it to refresh the location of proofreading marks it’s displaying in your view.

If a text change affects the text inside a context object, call the updateRange(_:with:reason:forContextWithIdentifier:) method to report that change instead.

Source§

impl UIWritingToolsCoordinator

Methods declared on superclass NSObject.

Source

pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>

Source

pub unsafe fn new(mtm: MainThreadMarker) -> Retained<Self>

Methods from Deref<Target = NSObject>§

Source

pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !

Handle messages the object doesn’t recognize.

See Apple’s documentation for details.

Methods from Deref<Target = AnyObject>§

Source

pub fn class(&self) -> &'static AnyClass

Available on crate feature UIIndirectScribbleInteraction only.

Dynamically find the class of this object.

§Panics

May panic if the object is invalid (which may be the case for objects returned from unavailable init/new methods).

§Example

Check that an instance of NSObject has the precise class NSObject.

use objc2::ClassType;
use objc2::runtime::NSObject;

let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Source

pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
where T: Encode,

👎Deprecated: this is difficult to use correctly, use Ivar::load instead.
Available on crate feature UIIndirectScribbleInteraction only.

Use Ivar::load instead.

§Safety

The object must have an instance variable with the given name, and it must be of type T.

See Ivar::load_ptr for details surrounding this.

Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: DowncastTarget,

Available on crate feature UIIndirectScribbleInteraction only.

Attempt to downcast the object to a class of type T.

This is the reference-variant. Use Retained::downcast if you want to convert a retained object to another type.

§Mutable classes

Some classes have immutable and mutable variants, such as NSString and NSMutableString.

When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.

So using this method to convert a NSString to a NSMutableString, while not unsound, is generally frowned upon unless you created the string yourself, or the API explicitly documents the string to be mutable.

See Apple’s documentation on mutability and on isKindOfClass: for more details.

§Generic classes

Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.

You can, however, safely downcast to generic collections where all the type-parameters are AnyObject.

§Panics

This works internally by calling isKindOfClass:. That means that the object must have the instance method of that name, and an exception will be thrown (if CoreFoundation is linked) or the process will abort if that is not the case. In the vast majority of cases, you don’t need to worry about this, since both root objects NSObject and NSProxy implement this method.

§Examples

Cast an NSString back and forth from NSObject.

use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};

let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();

Try (and fail) to cast an NSObject to an NSString.

use objc2_foundation::{NSObject, NSString};

let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());

Try to cast to an array of strings.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();

This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.

Downcast when processing each element instead.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);

for elem in arr {
    if let Some(data) = elem.downcast_ref::<NSString>() {
        // handle `data`
    }
}

Trait Implementations§

Source§

impl AsRef<AnyObject> for UIWritingToolsCoordinator

Source§

fn as_ref(&self) -> &AnyObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<NSObject> for UIWritingToolsCoordinator

Source§

fn as_ref(&self) -> &NSObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<UIWritingToolsCoordinator> for UIWritingToolsCoordinator

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<AnyObject> for UIWritingToolsCoordinator

Source§

fn borrow(&self) -> &AnyObject

Immutably borrows from an owned value. Read more
Source§

impl Borrow<NSObject> for UIWritingToolsCoordinator

Source§

fn borrow(&self) -> &NSObject

Immutably borrows from an owned value. Read more
Source§

impl ClassType for UIWritingToolsCoordinator

Source§

const NAME: &'static str = "UIWritingToolsCoordinator"

The name of the Objective-C class that this type represents. Read more
Source§

type Super = NSObject

The superclass of this class. Read more
Source§

type ThreadKind = dyn MainThreadOnly

Whether the type can be used from any thread, or from only the main thread. Read more
Source§

fn class() -> &'static AnyClass

Get a reference to the Objective-C class that this type represents. Read more
Source§

fn as_super(&self) -> &Self::Super

Get an immutable reference to the superclass.
Source§

impl Debug for UIWritingToolsCoordinator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for UIWritingToolsCoordinator

Source§

type Target = NSObject

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Hash for UIWritingToolsCoordinator

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Message for UIWritingToolsCoordinator

Source§

fn retain(&self) -> Retained<Self>
where Self: Sized,

Increment the reference count of the receiver. Read more
Source§

impl NSObjectProtocol for UIWritingToolsCoordinator

Source§

fn isEqual(&self, other: Option<&AnyObject>) -> bool
where Self: Sized + Message,

Check whether the object is equal to an arbitrary other object. Read more
Source§

fn hash(&self) -> usize
where Self: Sized + Message,

An integer that can be used as a table address in a hash table structure. Read more
Source§

fn isKindOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of the class, or one of its subclasses. Read more
Source§

fn is_kind_of<T>(&self) -> bool
where T: ClassType, Self: Sized + Message,

👎Deprecated: use isKindOfClass directly, or cast your objects with AnyObject::downcast_ref
Check if the object is an instance of the class type, or one of its subclasses. Read more
Source§

fn isMemberOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of a specific class, without checking subclasses. Read more
Source§

fn respondsToSelector(&self, aSelector: Sel) -> bool
where Self: Sized + Message,

Check whether the object implements or inherits a method with the given selector. Read more
Source§

fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
where Self: Sized + Message,

Check whether the object conforms to a given protocol. Read more
Source§

fn description(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object. Read more
Source§

fn debugDescription(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object to use when debugging. Read more
Source§

fn isProxy(&self) -> bool
where Self: Sized + Message,

Check whether the receiver is a subclass of the NSProxy root class instead of the usual NSObject. Read more
Source§

fn retainCount(&self) -> usize
where Self: Sized + Message,

The reference count of the object. Read more
Source§

impl PartialEq for UIWritingToolsCoordinator

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RefEncode for UIWritingToolsCoordinator

Source§

const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF

The Objective-C type-encoding for a reference of this type. Read more
Source§

impl UIInteraction for UIWritingToolsCoordinator

Source§

unsafe fn view(&self) -> Option<Retained<UIView>>
where Self: Sized + Message,

Available on crate features UIResponder and UIView and UIInteraction only.
Source§

unsafe fn willMoveToView(&self, view: Option<&UIView>)
where Self: Sized + Message,

Available on crate features UIResponder and UIView and UIInteraction only.
Source§

unsafe fn didMoveToView(&self, view: Option<&UIView>)
where Self: Sized + Message,

Available on crate features UIResponder and UIView and UIInteraction only.
Source§

impl DowncastTarget for UIWritingToolsCoordinator

Source§

impl Eq for UIWritingToolsCoordinator

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<'a, T> MainThreadOnly for T
where T: ClassType<ThreadKind = dyn MainThreadOnly + 'a> + ?Sized,

Source§

fn mtm(&self) -> MainThreadMarker

Get a MainThreadMarker from the main-thread-only object. Read more
Source§

fn alloc(mtm: MainThreadMarker) -> Allocated<Self>
where Self: Sized + ClassType,

Allocate a new instance of the class on the main thread. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,