Typography
Native typography composition, package imports, and editable Swift source.
Preview


Use it
Import SwiftCN to use CNTypography directly. This complete example also runs in the native component gallery.
import SwiftUI
import SwiftCN
struct TypographyExample: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
VStack(alignment: .leading, spacing: 8) {
CNTypography("Native by design", style: .title)
CNTypography("A shared styling language for SwiftUI.", style: .lead)
CNTypography("Keep your content, bindings, and gestures native.")
CNTypography("import SwiftCN", style: .code)
}
}
}
}Native behavior
Style localized native Text with title, heading, body, lead, small, muted, or code variants. Title and heading variants expose the header accessibility trait. Code uses native monospaced font design.
Style and compose
Visual recipes use .tw classes. Append classes: to override a recipe locally, or change the recipe in TWGlobalRules.named for your subtree. Native layout and interaction modifiers remain SwiftUI APIs. For control colors, append cn-tint-[primary], another theme token, or an interpolated Color.
Motion recipes are editable too: control-motion uses a 150 ms smooth response, disclosure-motion uses a 250 ms spring, and feedback-motion uses a 200 ms smooth response. Set a recipe to animate-none in your global rules to turn it off. Presets respect Reduce Motion. Static components stay still; native scrolling, drag tracking, and platform presentation keep their own gestures.
Action groups and card, dialog, and questionnaire footers stack when their content does not fit. The native layout preserves the same child controls during this change.
View builders preserve native child content. Native Button roles, labels, keyboard shortcuts, submit actions, and presentation modifiers still work. A string recipe does not replace a native control API or a typed chart mark.
Own the source
Copy the complete file below into your app alongside import SwiftCN. The file contains related composable parts from the same family. Its dependencies use public package APIs, so copied and imported components can coexist.
To own the styling engine too, download the complete source bundle. It contains every component, the .tw engine, its recipes, and the MIT license. Keep those files in one module and remove import SwiftCN from the usage example.
import SwiftUI
import SwiftCN
public enum CNTypographyStyle: String, CaseIterable, Sendable {
case largeTitle, title, heading, body, lead, small, muted, code
public var classes: TWClasses {
switch self {
case .largeTitle: "text-3xl font-bold"
case .title: "text-2xl font-bold"
case .heading: "text-xl font-semibold"
case .body: "text-base"
case .lead: "text-lg text-mutedForeground"
case .small: "text-sm font-medium"
case .muted: "text-sm text-mutedForeground"
case .code: "cn-mono text-sm bg-muted rounded-sm px-1"
}
}
}
public struct CNTypography: View {
private let text: LocalizedStringKey
private let style: CNTypographyStyle
private let classes: TWClasses
public init(_ text: LocalizedStringKey, style: CNTypographyStyle = .body, classes: TWClasses = "") {
self.text = text; self.style = style; self.classes = classes
}
public var body: some View {
Text(text).tw(cn("typography", style.classes, classes)).cnTextUtilities()
.accessibilityAddTraits(style == .largeTitle || style == .title || style == .heading ? .isHeader : [])
}
}
public struct CNSeparator: View {
private let axis: Axis
private let classes: TWClasses
public init(_ classes: TWClasses = "", axis: Axis = .horizontal) { self.classes = classes; self.axis = axis }
public var body: some View {
Divider().hidden().frame(maxHeight: axis == .vertical ? .infinity : nil)
.tw(cn("separator", axis == .horizontal ? "h-[1] w-full" : "w-[1]", classes)).accessibilityHidden(true)
}
}
public struct CNAspectRatio<Content: View>: View {
private let ratio: CGFloat
private let classes: TWClasses
private let content: Content
public init(_ ratio: CGFloat = 16 / 9, classes: TWClasses = "", @ViewBuilder content: () -> Content) {
precondition(ratio.isFinite && ratio > 0, "Aspect ratio must be finite and positive.")
self.ratio = ratio; self.classes = classes; self.content = content()
}
public var body: some View {
GeometryReader { proxy in content.frame(width: proxy.size.width, height: proxy.size.height) }
.tw(cn("cn-aspect-[\(ratio)]", classes)).cnAspectUtilities()
}
}
public struct CNDirection<Content: View>: View {
private let direction: LayoutDirection
private let content: Content
public init(_ direction: LayoutDirection, @ViewBuilder content: () -> Content) {
self.direction = direction; self.content = content()
}
public var body: some View { content.environment(\.layoutDirection, direction) }
}
public struct CNAvatar<Fallback: View>: View {
private let url: URL?
private let accessibilityLabel: String
private let classes: TWClasses
private let fallback: Fallback
public init(url: URL? = nil, accessibilityLabel: String, classes: TWClasses = "", @ViewBuilder fallback: () -> Fallback) {
self.url = url; self.accessibilityLabel = accessibilityLabel; self.classes = classes; self.fallback = fallback()
}
public var body: some View {
AsyncImage(url: url) { phase in
if let image = phase.image { image.resizable().tw("cn-avatar-image w-full") }
else { fallback }
}.tw(cn("avatar", classes)).cnAvatarUtilities().accessibilityElement(children: .ignore).accessibilityLabel(accessibilityLabel)
}
}