How do you use AttributedString
Asked on 08/03/2024
1 search
To use AttributedString
in your app, you can refer to several sessions from WWDC that cover different aspects of working with attributed strings. Here are some key points and sessions that might help:
-
Custom Visual Effects with SwiftUI:
- You can implement custom text attributes using the new text attribute protocol introduced alongside the text renderer in iOS 18. This allows you to pass data from your text to your text renderer and apply custom attributes to specific ranges of text.
- Example: Marking words with a custom emphasis attribute and iterating over the text layout to apply effects.
- Create custom visual effects with SwiftUI (18:31)
-
Build Multilingual-Ready Apps:
- You can add color attributes to an attributed string to highlight parts of the text. This is useful for differentiating text without relying on font weight, which may not be supported in all languages.
- Example: Highlighting search results using color attributes.
- Build multilingual-ready apps (07:09)
-
What’s New in AppKit:
- Text highlighting can be controlled using attributed string attributes. The new
texthighlight
attribute corresponds to atexthighlightstyle
, which can be set to system default or customized with a color scheme. - Example: Using the app's accent color for text highlighting.
- What’s new in AppKit (09:36)
- Text highlighting can be controlled using attributed string attributes. The new
-
Build a Spatial Drawing App with RealityKit:
- Using
AttributedString
makes it easy to add additional lines of text with different properties, such as different fonts and sizes. - Example: Centering text with a paragraph style and customizing 3D text models.
- Build a spatial drawing app with RealityKit (27:13)
- Using
-
Get Started with Dynamic Type:
- In UIKit, you can use
NSAttributedString
to create attributed strings with images asNSTextAttachment
and append them to the attributed string. - Example: Wrapping text under icons and resizing images based on text size.
- Get started with Dynamic Type (10:04)
- In UIKit, you can use
-
Bring Expression to Your App with Genmoji:
- You can decompose and recompose attributed strings to include image glyphs, which can be displayed in various contexts, including HTML.
- Example: Adding a Genmoji to a title and displaying it in a web interface.
- Bring expression to your app with Genmoji (05:55)
These sessions provide a comprehensive overview of how to use AttributedString
in different contexts and platforms, offering practical examples and code snippets to help you implement these features in your app.

Build a spatial drawing app with RealityKit
Harness the power of RealityKit through the process of building a spatial drawing app. As you create an eye-catching spatial experience that integrates RealityKit with ARKit and SwiftUI, you’ll explore how resources work in RealityKit and how to use features like low-level mesh and texture APIs to achieve fast updates of the users’ brush strokes.

What’s new in UIKit
Explore everything new in UIKit, including tab and document launch experiences, transitions, and text and input changes. We’ll also discuss better-than-ever interoperability between UIKit and SwiftUI animations and gestures, as well as general improvements throughout UIKit.

What’s new in AppKit
Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.