How iOS 18 Writing Tools Preserve Formatting
Apple’s iOS 18 introduces system-wide Writing Tools powered by Apple Intelligence, enabling users to rewrite, proofread, and adjust the tone of their text across various apps. A common challenge with generative AI text modification is the loss of rich text elements, such as inline styling, custom fonts, and structural elements. iOS 18 solves this by using advanced attributed string parsing, semantic diffing algorithms, and deep integration with the system's native text layout engines to ensure that styling remains intact even when sentence structures change drastically.
Attributed String Decomposition
At the foundation of text processing in iOS is the
NSAttributedString (and Swift’s modernized
AttributedString) framework. When a user highlights a
complex paragraph and invokes Writing Tools, the system does not simply
extract raw plain text. Instead, it breaks the selection down into a
text run: a sequence of characters paired with a dictionary of styling
attributes, such as font family, weight, color, underline styles, and
kerning.
Before passing the text to the on-device Large Language Model (LLM), the operating system generates a metadata map that indexes where specific styles begin and end. This separates the semantic content from its visual presentation.
Semantic Anchor Mapping and Diffing
Generative models output plain tokens. To bridge the gap between plain text generation and rich text output, iOS 18 uses a semantic alignment and diffing mechanism.
When the LLM generates a rephrased version of a paragraph, the system compares the original text with the revised output:
- Named Entity and Style Alignment: If a specific word was bolded for emphasis in the original text, the system identifies the corresponding synonym or concept in the newly generated sentence and applies that same bold trait to the replacement word.
- Span Preservation: If an entire clause was italicized, the diffing algorithm identifies the structural equivalent of that clause in the new text and maps the italic range accordingly.
- Link Anchoring: Hyperlinks attached to specific phrases are tracked semantically so that the target URL reattaches to the appropriate anchor text in the revised paragraph.
Deep Integration with TextKit 2
The preservation process relies heavily on TextKit 2, Apple's modern text layout and rendering engine. TextKit 2 handles text as a stream of elements separated from the physical viewport layout.
Writing Tools hooks directly into TextKit 2’s document model. When changes occur:
- Non-Destructive Replacement: Instead of clearing a text container and repopulating it, TextKit 2 calculates deltas. It substitutes character ranges while inheriting the paragraph-level rules (such as line spacing, alignment, and indents) assigned to that specific text block.
- Typographic Consistency: If a paragraph contains mixed fonts or non-standard tracking, the layout engine ensures that newly inserted characters inherit the typographic environment of their immediate context rather than reverting to a system default font like plain San Francisco.
Isolating Structural Node Boundaries
Complex paragraphs often feature structural elements like bulleted lists, numbered steps, or blockquotes. In these scenarios, Writing Tools treats structural delimiters separately from inline content:
- List Markers and Indentation: The system does not feed the actual bullet symbols or tab markers into the rephrasing prompt. It isolates the text within each list item, rewrites the inner text, and reinserts it into the existing list node hierarchy.
- Paragraph Breaks: Paragraph-level attributes—such
as
lineHeightMultipleandparagraphSpacing—are anchored to line breaks. By modifying only the character buffers between those break points, the overall geometry and spacing of the document remain unaltered.
By combining token-level semantic tracking with the architecture of TextKit 2, iOS 18 ensures that text transformations feel natural while entirely preserving the visual hierarchy and design of the user's document.