Building rich SwiftUI text experiences
At a glance
| Item | Summary |
|---|---|
| Purpose | Build an editor for formatted text using SwiftUI text editor views and attributed strings. |
| App architecture | A Swift sample with the source-visible chain SampleRecipeEditorApp → RootView → AttributedStringModel → SwiftUI APIs. |
| Main patterns | Binding-based state propagation, Actor-isolated state |
| Project style | 17 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
└── SampleRecipeEditor/
└── SampleRecipeEditor/
├── SampleRecipeEditorApp.swift
├── AttributeScopes.swift
├── AttributedStringModel.swift
├── BackgroundView.swift
├── InspectorView.swift
├── NavigationView.swift
├── RecipeFormattingDefinition.swift
├── RecipeView.swift
├── IngredientList.swift
├── RecipeList.swift
├── DispatchingChanges.swift
└── EditableRecipeText.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 4 project/configuration file(s) and 32 source declaration(s).
Overall architecture
flowchart LR
N1["SampleRecipeEditorApp"]
N2["RootView"]
N3["AttributedStringModel"]
N4["SwiftUI APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift:12 — architecture anchor
@main
struct SampleRecipeEditorApp: App {
var body: some Scene {
WindowGroup {
RootView()
}
.modelContainer(for: Recipe.self)
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into SwiftUI.
Ownership and state
classDiagram
IngredientNameAttributes o-- AdaptiveImageGlyphAttribute : adaptiveImageGlyph
RecipeModelAttributes o-- CustomAttributes : custom
RecipeModelAttributes o-- FontAttribute : font
RecipeModelAttributes o-- IngredientNameAttributes : ingredientName
Ownership evidence
SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift:17 — stored dependency or nearest verified ownership anchor
struct IngredientNameAttributes: AttributeScope {
let adaptiveImageGlyph: AttributeScopes.SwiftUIAttributes.AdaptiveImageGlyphAttribute
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
IngredientNameAttributes |
AdaptiveImageGlyphAttribute (adaptiveImageGlyph) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
CustomAttributes (custom) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
FontAttribute (font) |
stores or receives | Initialized by the owner; the binding is immutable |
RecipeModelAttributes |
IngredientNameAttributes (ingredientName) |
stores or receives | Initialized by the owner; the binding is immutable |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Class and protocol design
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift:13 — representative type boundary
struct SampleRecipeEditorApp: App {
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
SampleRecipeEditorApp |
Application entry and top-level composition | App |
AttributedStringModel |
Feature data or observable state | Identifiable |
BackgroundView |
User-interface presentation and input forwarding | View |
InspectorView |
User-interface presentation and input forwarding | View |
RootView |
User-interface presentation and input forwarding | View |
RecipeView |
User-interface presentation and input forwarding | View |
IngredientNameAttributes |
Represents a feature value or composable behavior | AttributeScope |
RecipeModelAttributes |
Represents a feature value or composable behavior | AttributeScope |
RecipeEditorAttributes |
Represents a feature value or composable behavior | AttributeScope |
CustomAttributes |
Represents a feature value or composable behavior | AttributeScope |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
valueChanged (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:24) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
data (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:27) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
scope (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:29) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
initializeValue (SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:47) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
Reference code
SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift:24 — representative boundary
private var valueChanged: Bool = false
@Attribute(.externalStorage)
private var data: Data
private var scope: ScopeSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | SampleRecipeEditorApp |
The source’s App suffix makes this role explicit. |
| Feature data or observable state | AttributedStringModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | BackgroundView, InspectorView, RecipeView, RootView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Binding-based state propagation | SampleRecipeEditor/SampleRecipeEditor/IngredientList.swift:18 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Actor-isolated state | SampleRecipeEditor/SampleRecipeEditor/EditableRecipeText.swift:10 |
Actor annotations make the concurrency ownership boundary explicit. |
Naming conventions
- Types: App: SampleRecipeEditorApp; Model: AttributedStringModel; View: BackgroundView, InspectorView, RecipeView, RootView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
initializeValue,willSave,hash,addIngredient,constrain. - Files:
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift,SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift,SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift,SampleRecipeEditor/SampleRecipeEditor/InspectorView.swift,SampleRecipeEditor/SampleRecipeEditor/RecipeFormattingDefinition.swift,SampleRecipeEditor/SampleRecipeEditor/RecipeView.swift.
Architecture takeaways
SampleRecipeEditorAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, SwiftData, PhotosUI, UniformTypeIdentifiers through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
SampleRecipeEditor/SampleRecipeEditor/SampleRecipeEditorApp.swift |
SampleRecipeEditorApp |
SampleRecipeEditor/SampleRecipeEditor/AttributeScopes.swift |
IngredientNameAttributes, RecipeModelAttributes, RecipeEditorAttributes, CustomAttributes, ParagraphFormat, ParagraphFormattingAttribute, IngredientAttribute |
SampleRecipeEditor/SampleRecipeEditor/AttributedStringModel.swift |
AttributedStringModel, Scope |
SampleRecipeEditor/SampleRecipeEditor/BackgroundView.swift |
BackgroundView, ImageWithPlaceholder |
SampleRecipeEditor/SampleRecipeEditor/InspectorView.swift |
IngredientSuggestion, InspectorView |
SampleRecipeEditor/SampleRecipeEditor/NavigationView.swift |
RootView |
SampleRecipeEditor/SampleRecipeEditor/RecipeFormattingDefinition.swift |
RecipeFormattingDefinition, NormalizeFonts, IngredientsAreGreen, BoldUsesAccentColor |
SampleRecipeEditor/SampleRecipeEditor/RecipeView.swift |
RecipeView |
SampleRecipeEditor/SampleRecipeEditor/IngredientList.swift |
IngredientsList, NewIngredientRow, IngredientToggleStyle |
SampleRecipeEditor/SampleRecipeEditor/RecipeList.swift |
RecipeList, NameField |