Skip to content

Copy rich text, and set up the text interaction before a selection from code - #42

Merged
servercimen merged 12 commits into
feature/replace-transitionfrom
feature/selection-menu
Nov 24, 2026
Merged

servercimen merged 12 commits into
feature/replace-transitionfrom
feature/selection-menu

Conversation

@servercimen

Copy link
Copy Markdown
Owner

Part 12 of a stack of 13 pull requests. The base is #41 (feature/replace-transition). #31 (part 1) tells how to merge the stack.

On this stacked branch, swift test passes (386 tests), and the iOS build and build-for-testing pass.

Summary

Copy put only plain text on the pasteboard. A paste into a rich text editor then lost the headings, bold, italic, code and links. Also, selectsAllWhenShown, or an app that calls becomeFirstResponder() itself, made the view first responder before its text interaction existed.

  • copy(_:) writes one pasteboard item with two types. public.utf8-plain-text is the same text as before (copyText(in:)). public.rtf has the same characters, with the fonts of the style for headings, bold, italic, inline code and code blocks, and with links, strikethrough and underline. Text in table cells keeps its bold, italic, code and links. The RTF has no colors, so pasted text takes the colors of the place it goes to.
  • The RTF names only public fonts. Helvetica Neue takes the place of the system font, and Menlo the place of the monospaced system font, each with the same size, bold and italic. Fonts that the app sets by name do not change.
  • becomeFirstResponder() adds the UITextInteraction before the view becomes first responder.
  • The plain text and the rich text come from one walker, so their characters are always the same. The list and quote prefixes go in with one linear pass.
  • The edit menu does not change. It already offers Look Up, as the menu of a non-editable UITextView does.
  • The README tells what Copy writes.

Tests

  • swift test on macOS: 186 tests pass. The tests in RichCopyTests.swift check that the rich text has the characters of copyText(in:), and check the fonts, links, strikethrough, underline, partial spans and table cells. They read the RTF back, and check that each font has a public name. A test for the last commit checks bold and italic code in the monospaced system font. It failed before that commit.
  • iOS: xcodebuild build -scheme TranscriptKit -destination 'generic/platform=iOS' at the last commit: the build succeeds with no warnings.

Checked on an iPhone 12 mini

iOS 27.0, in a check screen in a local test app. The phone check ran on the commit before the last one. The first runs were on main. Main showed the rich copy problem. Look Up, the selection rule and the handles behaved the same on main.

  • Rich copy: main writes only public.utf8-plain-text, and a paste into a rich text view is plain text in one font. The branch writes both types in one item, and the plain text equals copyText(in:). A paste keeps the 25.5 pt bold heading, bold, italic, monospaced code and the link. The RTF names only public fonts: Helvetica Neue faces, Menlo, and Avenir for a style that names it.
  • Bold and italic code with a monospaced system code font: that build lost them in the RTF. The last commit fixes this, and a unit test covers it. The phone did not run the fix.
  • Look Up: the edit menu offers it on main, and still offers it on the branch.
  • One view in a window has a selection at a time, and Share and Translate keep the selection. This is the same on main and on the branch.
  • Handles after selectsAllWhenShown: on main, the view becomes first responder without a text interaction, but the handles still show in all 5 variants (UIKit before and after the window, SwiftUI, a stream, a sheet). So the phone did not show that the change is necessary. The branch shows the same handles.

Stand-ins: the menus open with UIMenuController.showMenu, and Copy, Share and Translate go to the first responder with UIApplication.sendAction. The check reads the visible handles from UITextSelectionDisplayInteraction, and screenshots confirm them. The paste goes into an editable UITextView in the test app.

Not checked

  • A paste on the phone with the last commit, with a monospaced system code font.
  • iOS versions before 27.0, to see if the handles are missing there without this change.
  • A paste into other apps.

🤖 Generated with Claude Code

servercimen and others added 11 commits November 23, 2026 17:03
selectsAllWhenShown called becomeFirstResponder() before the
UITextInteraction existed. The interaction then did not show the
selection handles for the selection that came next.

becomeFirstResponder() now adds the interaction first. This covers
selectsAllWhenShown and an app that calls becomeFirstResponder() itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy wrote only plain text. A paste into a rich-text editor lost the
fonts, bold, italic and links.

Copy now writes one pasteboard item with two types. The plain text is
the same text as before. The RTF has the same characters, with the fonts
of the style, links, strikethrough and underline, and no colors.

copyText(in:) and the rich text use one walker, so both always have the
same text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The RTF has the list markers, quote prefixes, tables and rules of the
plain text as characters, not as RTF lists or tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rich copy tests used NSUnderlineStyle with only AppKit imported, so
the test target did not build for iOS. The file now imports UIKit where
it can, as RichCopy.swift does.

The tests now also check underline, and they check that strikethrough
and underline stay when the RTF is read back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rich copy of a table was the plain Markdown table in the block font.
Bold, italic, code and links in the cells were lost. Each cell is now
styled from its spans, and the pipes and the separator row go between
the cells. The plain text does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The list and quote prefixes went in with one insert per line, from the
last line to the first. Each insert moved the rest of the text, so a
long code block in a list item or a quote took time in proportion to
its lines times its length. The copy now appends each prefix and line
to a new string, so the time is linear. The text does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The comment said that the interaction shows the selection only if it
is in place when the view becomes first responder. A check on iOS 27.0
showed the handles also when the view became first responder before
the interaction existed. The code still adds the interaction first, so
the view is never first responder without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The RTF named the system font by its private names, for example
.SFUI-Semibold. An app that finds a font by its name in the RTF does
not find these names, and shows another font, such as Times.

Copy now changes each font with a private name to Helvetica Neue with
the same size, bold and italic, or to Menlo for monospaced text. Fonts
that the app sets by name do not change. A UIKit reader that knew the
system font now gets Helvetica Neue in its place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Some apps find the system font by its private name, and some do not.
The doc comment and the README now say that some apps do not find it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the code font was the monospaced system font, the copy changed
it to Menlo-Regular before it read the bold and italic traits, so code
in bold or italic lost them in a paste. Now the copy picks Menlo and
then applies the traits, as it does for the other text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TranscriptTextView.copy(_:): the copy runs on the main thread from any thread, copies selectedRange, and puts plain and rich text on the pasteboard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@servercimen
servercimen added this pull request to stack #44 November 24, 2026 20:57
@servercimen
servercimen removed this pull request from stack #44 November 24, 2026 21:01
@servercimen
servercimen added this pull request to stack #45 November 24, 2026 21:01
@servercimen
servercimen merged commit 4ef3934 into main Nov 24, 2026
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant