Skip to main content
MarkdownTextView is the primary view component for rendering markdown content with support for syntax highlighting, math equations, tables, and interactive links.

Class Definition

public final class MarkdownTextView: UIView // iOS
public final class MarkdownTextView: NSView // macOS

Initialization

viewProvider
ReusableViewProvider
default:".init()"
Optional provider for recycling views like code blocks and tables. Creates a new provider if not specified.
public init(viewProvider: ReusableViewProvider = .init())

Properties

Interactive Handlers

Callback invoked when a link is tapped. Receives the link payload (URL or string), text range, and tap location.
imageTapHandler
((String, CGPoint) -> Void)?
Callback invoked when an image is tapped. Receives the image source URL and tap location.
codePreviewHandler
((String?, NSAttributedString) -> Void)?
Callback invoked for code block previews. Receives the optional language identifier and attributed code content.

Appearance

theme
MarkdownTheme
default:".default"
The theme used to style markdown elements including fonts, colors, and spacing. Setting this property triggers a re-render.

Content

document
PreprocessedContent
The currently rendered preprocessed markdown content. This property is read-only.
textView
LTXLabel
The underlying text view that displays the rendered content. This property is read-only.

Performance

throttleInterval
TimeInterval?
default:"1/20"
Throttle interval for content updates in seconds. Defaults to 20 FPS (0.05 seconds). Set to nil to disable throttling.
trackedScrollView
UIScrollView?
The scroll view tracked for selection updates. Set via bindContentOffset(from:). Read-only.

Methods

setMarkdown

Set Preprocessed Content

Sets preprocessed markdown content for rendering. This method uses the internal Combine pipeline with throttling.
public func setMarkdown(_ content: PreprocessedContent)
content
PreprocessedContent
required
The preprocessed markdown content to render.

Set Raw Markdown String

Sets raw markdown string. Parsing, syntax highlighting, and math rendering are performed on a background queue. Cancels any in-flight preprocessing when new content arrives.
public func setMarkdown(string: String)
string
String
required
The raw markdown string to parse and render.

setMarkdownManually

Sets preprocessed content directly, bypassing the Combine pipeline. Use this for synchronous updates without throttling.
public func setMarkdownManually(_ content: PreprocessedContent)
content
PreprocessedContent
required
The preprocessed markdown content to render immediately.
This method must be called on the main thread.

reset

Resets the view to an empty state, clearing all content and cached rendering data.
public func reset()
This method must be called on the main thread.

bindContentOffset

Binds text selection updates to a scroll view’s content offset changes.
public func bindContentOffset(from scrollView: UIScrollView?) // iOS
public func bindContentOffset(from scrollView: NSScrollView?) // macOS
scrollView
UIScrollView? / NSScrollView?
required
The scroll view to track for selection updates, or nil to unbind.

boundingSize

Calculates the bounding size for the markdown content at a given width.
public func boundingSize(for width: CGFloat) -> CGSize
width
CGFloat
required
The maximum width constraint.
size
CGSize
The calculated size that fits the content.

Platform Support

MarkdownTextView is available on:
  • iOS (inherits from UIView)
  • macOS (inherits from NSView)
  • visionOS (via UIView)

Example

let markdownView = MarkdownTextView()
markdownView.theme = .default
markdownView.linkHandler = { payload, range, location in
    if case .url(let url) = payload {
        UIApplication.shared.open(url)
    }
}

markdownView.setMarkdown(string: "# Hello\n\nThis is **markdown** content.")

Build docs developers (and LLMs) love