AttributedString patterns for rich text formatting, alignment, selection, and SwiftUI integration. Use when working with styled text, text editing, or AttributedString APIs.
npx skills add https://github.com/rshankras/claude-code-apple-skills --skill attributed-string
Correct API shapes and patterns for Foundation's AttributedString. Covers creating styled text, applying attributes to ranges, text alignment, writing direction, line height control, text selection and editing, discontiguous substrings, and SwiftUI integration.
Use this skill when the user:
What do you need with AttributedString?
|
+-- Create or style text
| |
| +-- Simple inline attributes (font, color)
| | --> Creating and Styling section
| |
| +-- Paragraph-level formatting (alignment, line height)
| --> Text Alignment and Formatting section
|
+-- Control text layout
| |
| +-- Writing direction (LTR / RTL)
| | --> Writing Direction and Line Height section
| |
| +-- Line spacing / height
| --> Writing Direction and Line Height section
|
+-- Edit or select text programmatically
| |
| +-- Replace selection with characters or AttributedString
| | --> Text Selection and Editing section
| |
| +-- Work with multiple non-contiguous ranges
| --> DiscontiguousAttributedSubstring section
|
+-- Display in SwiftUI
--> SwiftUI Integration section
| API | Minimum Version | Notes |
|-----|----------------|-------|
| AttributedString | iOS 15 / macOS 12 | Swift-native replacement for NSAttributedString |
| AttributedString.paragraphStyle | iOS 15 / macOS 12 | Uses NSMutableParagraphStyle |
| AttributedString.writingDirection | iOS 26 / macOS 26 | New in 2025 |
| AttributedString.LineHeight | iOS 26 / macOS 26 | .exact(points:), .multiple(factor:), .loose |
| AttributedString.alignment | iOS 26 / macOS 26 | .left, .center, .right |
| AttributedTextSelection | iOS 26 / macOS 26 | Programmatic text selection |
| replaceSelection(_:withCharacters:) | iOS 26 / macOS 26 | Replace selection with plain characters |
| replaceSelection(_:with:) | iOS 26 / macOS 26 | Replace selection with AttributedString |
| DiscontiguousAttributedSubstring | iOS 26 / macOS 26 | Non-contiguous range selections |
| AttributedString.utf8 | iOS 26 / macOS 26 | UTF-8 code unit view |
| TextEditor(text:selection:) with AttributedString | iOS 26 / macOS 26 | SwiftUI rich text editing |
| .textSelectionAffinity(_:) | iOS 26 / macOS 26 | Control cursor affinity at line boundaries |
| # | Mistake | Fix |
|---|---------|-----|
| 1 | Using NSAttributedString in new Swift code | Use AttributedString (iOS 15+) for type-safe, Swift-native attributes |
| 2 | Applying range-based attributes without checking the range exists | Always safely unwrap the result of text.range(of:) before subscripting |
| 3 | Forgetting that AttributedString is a value type | Mutations require var, not let; assign attributes after declaring as var |
| 4 | Building NSMutableParagraphStyle when new alignment API is available | Use text.alignment = .center on iOS 26+ instead of manual paragraph styles |
| 5 | Modifying the original string instead of the selection when using replaceSelection | Pass the selection as inout and let the API update the selection range for you |
// Plain text
let plain = AttributedString("Hello, world!")
// With attributes applied inline
var bold = AttributedString("Bold text")
bold.font = .boldSystemFont(ofSize: 16)
var text = AttributedString("Styled text")
text.foregroundColor = .red
text.backgroundColor = .yellow
text.font = .systemFont(ofSize: 14)
// Attribute on a specific range
if let range = text.range(of: "Styled") {
text[range].underlineStyle = .single
text[range].underlineColor = .blue
}
let source = AttributedString("Hello, world!")
if let range = source.range(of: "world") {
let substring = source[range]
let extracted = AttributedString(substring) // standalone copy
}
| Pattern | Verdict |
|---------|---------|
| var text = AttributedString("...") then mutate | Correct |
| let text = AttributedString("...") then mutate | Will not compile -- value type requires var |
| Force-unwrapping text.range(of:)! | Fragile -- use if let or guard let |
var paragraph = AttributedString("Centered paragraph of text")
let style = NSMutableParagraphStyle()
style.alignment = .center
paragraph.paragraphStyle = style
var paragraph = AttributedString("Centered paragraph of text")
paragraph.alignment = .center
Available TextAlignment values:
| Value | Description |
|-------|-------------|
| .left | Left-aligned text |
| .right | Right-aligned text |
| .center | Center-aligned text |
var text = AttributedString("Hello عربي")
text.writingDirection = .rightToLeft
| Value | Description |
|-------|-------------|
| .leftToRight | Standard LTR layout |
| .rightToLeft | RTL layout for Arabic, Hebrew, etc. |
var multiline = AttributedString(
"This is a paragraph\nwith multiple lines\nof text."
)
// Exact point value
multiline.lineHeight = .exact(points: 32)
// Multiplier of the default line height
multiline.lineHeight = .multiple(factor: 2.5)
// System-defined loose spacing
multiline.lineHeight = .loose
| Mode | Use Case |
|------|----------|
| .exact(points:) | Pixel-perfect designs with fixed line heights |
| .multiple(factor:) | Proportional scaling relative to font size |
| .loose | Comfortable reading spacing chosen by the system |
var text = AttributedString("Here is my dog")
var selection = AttributedTextSelection(range: text.range(of: "dog")!)
// Replace with plain characters
text.replaceSelection(&selection, withCharacters: "cat")
// Replace with an AttributedString
let replacement = AttributedString("horse")
text.replaceSelection(&selection, with: replacement)
Key point: use withCharacters: for plain text, with: for styled replacements.
Select and manipulate multiple non-contiguous ranges at once (iOS 26+).
let text = AttributedString("Select multiple parts of this text")
if let range1 = text.range(of: "Select"),
let range2 = text.range(of: "text") {
let rangeSet = RangeSet([range1, range2])
var substring = text[rangeSet] // DiscontiguousAttributedSubstring
substring.backgroundColor = .yellow
// Flatten into a single contiguous AttributedString
let combined = AttributedString(substring)
}
Access raw UTF-8 code units (iOS 26+):
let text = AttributedString("Hello")
for codeUnit in text.utf8 {
print(codeUnit)
}
struct SuggestionTextEditor: View {
@State var text: AttributedString = ""
@State var selection = AttributedTextSelection()
var body: some View {
VStack {
TextEditor(text: $text, selection: $selection)
SuggestionsView(
substrings: getSubstrings(
text: text,
indices: selection.indices(in: text)
)
)
}
}
}
Control which line the cursor appears on when positioned at a line boundary:
TextEditor(text: $text, selection: $selection)
.textSelectionAffinity(.upstream)
.upstream keeps the cursor at the end of the previous line; .downstream moves it to the start of the next.
// Simple display
Text(attributedString)
// With text selection enabled
Text(attributedString)
.textSelection(.enabled)
AttributedString (not NSAttributedString) for new Swift codeif let or guard letvar before applying attributesreplaceSelection passes selection as inout (&selection)if #available) wrap newer APIs when supporting older deployment targetsTextEditor(text:selection:) overload used for rich text editing (iOS 26+).textSelectionAffinity applied where cursor behavior at line wraps matters.textSelection(.enabled) added to Text views that display user contentGuide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision docs, or similar structured content. This workflow helps users efficiently transfer context, refine content through iteration, and verify the doc works for readers. Trigger when user mentions writing docs, creating proposals, drafting specs, or similar documentation tasks.
Automatically creates user-facing changelogs from git commits by analyzing commit history, categorizing changes, and transforming technical commits into clear, customer-friendly release notes. Turns hours of manual changelog writing into minutes of automated generation.
Use when implementing any feature or bugfix, before writing implementation code
Use when you have a spec or requirements for a multi-step task, before touching code
Use when creating new skills, editing existing skills, or verifying skills work before deployment
Use when writing or improving README files. Not all READMEs are the same — provides templates and guidance matched to your audience and project type.
| Remove signs of AI-generated writing from text. Use when editing or reviewing text to make it sound more natural and human-written. Based on Wikipedia's inflated symbolism, promotional language, superficial -ing analyses, vague attributions, em dash overuse, rule of three, AI vocabulary words, negative parallelisms, and excessive conjunctive phrases.
Official Opentrons Protocol API for OT-2 and Flex robots. Use when writing protocols specifically for Opentrons hardware with full access to Protocol API v2 features. Best for production Opentrons protocols, official API compatibility. For multi-vendor automation or broader equipment control use pylabrobot.
Take rshankras/attributed-string from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.