Search Docs
Loading...
Skip to content

Copy and Paste Formatted Text

Copy and paste text with its formatting between text blocks and other apps, and read or write a range of a text block as HTML.

6 mins
estimated time
GitHub

Text blocks read and write their formatted content as HTML. engine.block.getTextAsHTML(_:in:) returns a range as HTML with inline CSS. The HTML states the color, background color, weight, italic, decorations, script style, case, font size and typeface of each run. It also states the list style, list level and alignment of each paragraph. engine.block.replaceTextWithHTML(_:html:in:) replaces a range with the text and the formatting of HTML. This is the same format that browsers and other apps put on the pasteboard.

Both calls take an optional Range<String.Index> of the block’s text. Passing nil, outside text edit mode, uses the whole text. Plain text editing is in Edit Text.

Using the Built-in Copy and Paste#

In text edit mode, the system copy, cut and paste actions keep the formatting of the selected text. A copy puts both the plain text and the HTML on the system pasteboard, so a paste into another app also keeps the formatting. A paste reads the HTML of the pasteboard. Rich text from apps such as Notes, Pages and TextEdit is converted to HTML first. A pasteboard without formatted text, or with HTML that holds no text, inserts the plain text. Paste and Match Style pastes the plain text in the style at the cursor. Integrations that let users edit text on the canvas need no code for this.

Canva reads only the plain text of a copy from CE.SDK, because Canva ignores the formatting of other apps.

In the iOS editor, outside text edit mode, text from another app pastes on the canvas as a new text block, as Copy, Cut and Paste in the canvas menu guide describes.

Copy Formatted Text Between Blocks#

Read the formatted text of the source block with engine.block.getTextAsHTML(_:in:).

// Copy the whole text with every run and paragraph format
let copiedHTML = try engine.block.getTextAsHTML(source)

Write it to another block with engine.block.replaceTextWithHTML(_:html:in:). HTML from getTextAsHTML(_:in:) sets every format it carries, including the typeface and the font size. The typeface applies only when a typeface of the scene holds its font file. Otherwise the text keeps the typeface at the insert position, with the weight and style of the source.

// Replace the whole text of the target block with the copied text
try engine.block.replaceTextWithHTML(target, html: copiedHTML)

To copy a part of a block, pass a subrange to getTextAsHTML(_:in:). An empty range returns an empty string. To insert without replacing text, pass an empty range, such as endIndex ..< endIndex of the block’s text from getString(_:property:). The example copies the red title and inserts it at the end of the headline. title is the range of “Summer Sale” in the text of the source block.

// Copy only "Summer Sale" and insert it at the end of the headline
let titleHTML = try engine.block.getTextAsHTML(source, in: title)
let headlineText = try engine.block.getString(headline, property: "text/text")
try engine.block.replaceTextWithHTML(
headline,
html: titleHTML,
in: headlineText.endIndex ..< headlineText.endIndex,
)

In text edit mode, passing nil makes both calls use the selection. replaceTextWithHTML(_:html:in:) then moves the cursor behind the inserted text.

Paste HTML From Other Apps#

Pass HTML from the pasteboard of another app, such as Google Docs, Word, a web page or an Apple app, to engine.block.replaceTextWithHTML(_:html:in:).

// HTML as a browser or a document app puts it on the clipboard, with its font size and line height
let clipboardHTML = #"<p style="line-height:1.5"><b style="font-size:24px">Bold</b> and "#
+ #"<span style="color:#ff0000">red</span></p><ul><li>item</li></ul>"#
try engine.block.replaceTextWithHTML(pasted, html: clipboardHTML)

HTML from another app sets these formats:

  • Weight, italic, decorations, script style and case.
  • Text color and background color.
  • Font size, letter spacing and line height.
  • Lists, list levels and alignment.

The pasted text takes the first font family of the HTML that a typeface of the asset sources has. Without such a typeface, it keeps the typeface of the block. A CSS px is one pixel of a scene whose font size unit is the pixel, so the size reads as in the source app. A gray, black or white that covers the largest part of the pasted text is the default color of the source app. It does not replace the color of the block, and the same rule applies to the background color. A pasted paragraph without a list keeps the list of the target paragraph. Markup that is not supported keeps only its text.

Paste and Match Style#

Paste and Match Style pastes only the plain text, which takes the style at the cursor, as typed text does. On iOS, it is in the edit menu. On macOS and Mac Catalyst, it is the Paste and Match Style item of the Edit menu. Your own input does the same with engine.block.replaceText(_:text:in:) and the plain text of the pasteboard.

Handle HTML Without Text#

When the HTML holds no text, such as a copied image, engine.block.replaceTextWithHTML(_:html:in:) throws an EngineError whose catalogCode is .blockTextHtmlHasNoText, and the block does not change. Catch it and insert the plain text of the pasteboard with engine.block.replaceText(_:text:in:). The Error Catalog lists all error codes.

// A copied image puts HTML without text on the clipboard
let imageHTML = #"<p><img src="https://example.com/photo.png"></p>"#
let imagePlainText = "Beach photo"
do {
try engine.block.replaceTextWithHTML(note, html: imageHTML)
} catch let error as EngineError where error.catalogCode == .blockTextHtmlHasNoText {
// The block is unchanged, so insert the plain text of the clipboard instead
try engine.block.replaceText(note, text: imagePlainText)
}

Scopes#

Without the text/character scope, replaceTextWithHTML(_:html:in:) inserts only the text, with the style at the insert position. Without the fill/change scope, or when the text color is bound to a variable, no color is pasted. When the font size or the typeface of the block is bound to a variable, it is not pasted. replaceTextWithHTML(_:html:in:) also requires the text/edit scope. Lock Design shows how to set scopes.

Troubleshooting#

Problem Cause and solution
Pasted text has the wrong typeface No typeface of the asset sources has the font family of the source app, so the text keeps the typeface of the block. Add that typeface to an asset source.
Pasted text is much smaller or larger than the block The paste keeps the font size of the source app. Use Paste and Match Style to paste the text in the style at the cursor.
A paste into Canva has no formatting Canva reads only the plain text of other apps.
Bold or italic does not show The typeface of the block has no bold or italic font. Use a typeface with these variants.
The paste throws EngineErrorCode.blockTextHtmlHasNoText The HTML holds no text. The example catches this error and inserts the plain text with engine.block.replaceText(_:text:in:).
The text color of the paste is not applied A gray, black or white that covers the largest part of the pasted text does not replace the color of the block. The same rule applies to the background color. A block without the fill/change scope, or with its text color bound to a variable, takes no color.
Only the text is pasted, without formatting The block does not allow the text/character scope.

API Reference#

Methods#

Method Description
engine.block.getTextAsHTML(_:in:) Get a range of a text block as HTML with its formatting
engine.block.replaceTextWithHTML(_:html:in:) Replace a range with the text and formatting of HTML
engine.block.replaceText(_:text:in:) Insert plain text, the fallback for HTML without text
engine.block.getString(_:property:) Read the plain text, for example to find the end index

Next Steps#

  • Edit Text - Replace, remove and format text ranges.
  • Text Styling - Apply fonts, colors, alignment and other styling options.
  • Text Decorations - Add underline, strikethrough and overline decorations.
  • Lock Design - Control which users can change text and its formatting.