AttributedString 有那些改變?
Asked on 06/12/2025
1 search
在 WWDC 2025 的 "Code-along: Cook up a rich text experience in SwiftUI with AttributedString" 會議中,AttributedString 有以下幾個改變和新功能:
-
支持多種字符視圖:AttributedString 現在提供了對 UTF8 和 UTF16 標量的視圖,這些視圖與現有的字符視圖共享相同的索引。
-
變換功能:引入了一個 transform 函數,允許在給定範圍內對 AttributedString 進行就地變換,並在變換結束後更新範圍的索引。
-
自定義屬性和約束:可以定義自定義的 AttributedString 屬性,並通過 AttributedStringKey 協議提供的 API 來限制屬性值的變化,確保屬性與文本內容保持一致。
-
支持豐富文本編輯:SwiftUI 的 Text Editor 現在支持 AttributedString,允許使用內建的文本格式化控件進行豐富文本編輯。
-
改進的索引管理:在變更 AttributedString 時,會自動更新索引以避免無效的選擇或崩潰。
這些改變使得 AttributedString 在 SwiftUI 中的應用更加靈活和強大,特別是在處理豐富文本和自定義文本格式時。
如果你想了解更多細節,可以查看 Code-along: Cook up a rich text experience in SwiftUI with AttributedString 的 "TextEditor and AttributedString" 章節。

Code-along: Cook up a rich text experience in SwiftUI with AttributedString
Learn how to build a rich text experience with SwiftUI’s TextEditor API and AttributedString. Discover how you can enable rich text editing, build custom controls that manipulate the contents of your editor, and customize the formatting options available. Explore advanced capabilities of AttributedString that help you craft the best text editing experiences.

What’s new in SwiftUI
Learn what’s new in SwiftUI to build great apps for any Apple platform. We’ll explore how to give your app a brand new look and feel with Liquid Glass. Discover how to boost performance with framework enhancements and new instruments, and integrate advanced capabilities like web content and rich text editing. We’ll also show you how SwiftUI is expanding to more places, including laying out views in three dimensions.

Enhance your app’s multilingual experience
Create a seamless experience for anyone who uses multiple languages. Learn how Language Discovery allows you to optimize your app using a person’s preferred languages. Explore advances in support for right-to-left languages, including Natural Selection for selecting multiple ranges in bidirectional text. We’ll also cover best practices for supporting multilingual scenarios in your app.