iOS Platform Guide
Installation
Add Mobile-Pretext as a Swift Package dependency:
// Package.swift
dependencies: [
.package(url: "https://github.com/MaTriXy/mobile-pretext.git", from: "1.0.0")
]
// Target
.product(name: "Pretext", package: "mobile-pretext")Or in Xcode: File → Add Package Dependencies → paste the repo URL.
Platform-Specific Details
Font Specification
iOS uses FontSpec instead of a CSS font string:
let font = FontSpec(name: "Helvetica Neue", size: 16)The name must match a font available on the system. Use the PostScript name for precise matching:
"Helvetica Neue"— system Helvetica"Inter"— if bundled in your app".SFUI-Regular"— San Francisco (not recommended — use the font descriptor API instead)
Measurement Engine
iOS uses CoreText (CTLine) for text measurement. CoreText is Apple's low-level text rendering framework and gives consistent results between measurement and rendering.
Unlike the browser version, no emoji width correction is needed — CoreText measures emoji the same way it renders them.
Word Segmentation
iOS uses NLTokenizer from the NaturalLanguage framework for word boundary detection. It handles:
- Thai, Lao, Khmer (dictionary-based, no spaces between words)
- CJK (character-level boundaries)
- Standard Latin word boundaries
Thread Safety
The measurement cache and segmenter state are protected with NSLock. You can safely call prepare() and layout() from any thread. However, for best performance, prepare texts on a background queue and layout on the main thread.
SwiftUI Integration
Height-only measurement
struct MessageCell: View {
let message: String
@State private var prepared: PreparedText?
let font = FontSpec(name: "Helvetica Neue", size: 16)
var body: some View {
GeometryReader { geo in
let result = prepared.map {
Pretext.layout($0, maxWidth: Double(geo.size.width), lineHeight: 22)
}
Text(message)
.font(.custom("Helvetica Neue", size: 16))
.frame(height: result?.height ?? 0)
}
.onAppear { prepared = Pretext.prepare(message, font: font) }
}
}Custom Canvas rendering
Canvas { context, size in
let result = Pretext.layoutWithLines(prepared, maxWidth: Double(size.width), lineHeight: 22)
for (i, line) in result.lines.enumerated() {
context.draw(
Text(line.text).font(.custom("Helvetica Neue", size: 16)),
at: CGPoint(x: 0, y: Double(i) * 22)
)
}
}DocC API Reference
The Swift library includes a DocC documentation catalog. To browse it in Xcode:
- Open the package in Xcode
- Product → Build Documentation
- Browse under "Pretext" in the documentation navigator
Or generate a static site:
cd ios
swift package generate-documentation --output-path ../docs/.vitepress/dist/api/iosSample App
The repo includes a full sample app with 5 demo sections:
open ios/SampleApp/PretextDemo.xcodeprojSet your development team in Signing & Capabilities, select your iPhone, and run.