View in English

  • Apple 开发者
    • 入门汇总

    探索“入门汇总”

    • 概览
    • 学习
    • Apple Developer Program

    及时了解最新动态

    • 最新动态
    • 开发者你好
    • 平台

    探索“平台”

    • Apple 平台
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    • App Store

    精选

    • 设计
    • 分发
    • 游戏
    • 配件
    • 网页
    • Home
    • CarPlay 车载
    • 技术

    探索“技术”

    • 概览
    • Xcode
    • Swift
    • SwiftUI

    精选

    • 辅助功能
    • App Intents
    • Apple 智能
    • 游戏
    • 机器学习与 AI
    • 安全性
    • Xcode Cloud
    • 社区

    探索“社区”

    • 概览
    • “与 Apple 会面交流”活动
    • 社区主导的活动
    • 开发者论坛
    • 开源

    精选

    • WWDC
    • Swift Student Challenge
    • 开发者故事
    • App Store 大奖
    • Apple 设计大奖
    • Apple Developer Centers
    • 文档

    探索“文档”

    • 文档库
    • 技术概述
    • 示例代码
    • 《人机界面指南》
    • 视频

    发布说明

    • 精选更新
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • Apple tvOS
    • Xcode
    • 下载

    探索“下载”

    • 所有下载
    • 操作系统
    • 应用程序
    • 设计资源

    精选

    • Xcode
    • TestFlight
    • 字体
    • SF Symbols
    • Icon Composer
    • 支持

    探索“支持”

    • 概览
    • 帮助指南
    • 开发者论坛
    • “反馈助理”
    • 联系我们

    精选

    • 《开发者账户帮助》
    • 《App 审核指南》
    • 《App Store Connect 帮助》
    • 即将实行的要求
    • 协议和准则
    • 系统状态
  • 快速链接

    • 活动
    • 新闻
    • 论坛
    • 示例代码
    • 视频
 

视频

打开菜单 关闭菜单
  • 专题
  • 所有视频
  • 关于

更多视频

  • 简介
  • 概要
  • 代码
  • 使用 TextKit 提升 App 的文本体验

    了解如何将内置文本视图的便利性与 TextKit 的控制优势相结合。我们将介绍如何借助新的 API,通过行号、可折叠部分等自定行为,轻松地扩展 UITextView 和 NSTextView。我们还将深入探索 TextKit 架构,并详细讲解文本附件的全新缓存和重用策略。为了充分从这个讲座中获益,我们建议你观看 WWDC21 视频“认识 TextKit 2”和 WWDC22 视频“TextKit 和文本视图的新功能”。

    章节

    • 0:00 - Introduction
    • 3:09 - TextKit architecture
    • 9:17 - What's new in TextKit
    • 11:27 - Extending framework text views
    • 12:58 - Example: Code editor with line numbers
    • 17:52 - Example: Collapsible recipe sections
    • 19:56 - Text attachments and view provider reuse
    • 23:00 - Next steps

    资源

    • Enriching your text in text views
    • TextKit
      • 高清视频
      • 标清视频

    相关视频

    WWDC26

    • 提升阅读 App 的辅助功能体验

    WWDC22

    • TextKit 和文本视图的新功能

    WWDC21

    • 认识 TextKit 2
  • 搜索此视频…
    • 9:47 - NSTextViewportRenderingSurface conformance

      class MyView: UIView, NSTextViewportRenderingSurface {}
    • 10:25 - NSTextViewportRenderingSurfaceKey and NSMapTable

      class MyView: UIView, NSTextViewportRenderingSurface {}
      
      var cache: NSMapTable<NSTextLayoutFragment, MyView>
    • 12:39 - UITextView/NSTextView in SwiftUI via ViewRepresentable

      // Using a TextView in SwiftUI
      
      import SwiftUI
      
      struct MyTextView: View {
          var body: some View { TextViewRepresentable() }
      }
      
      #if os(macOS)
      struct TextViewRepresentable: NSViewRepresentable {
          func makeNSView(context: Context) -> NSTextView { 
            NSTextView() 
          }
          func updateNSView(_ nsView: NSTextView, context: Context) {
          }
      }
      #else
      struct TextViewRepresentable: UIViewRepresentable {
          func makeUIView(context: Context) -> UITextView {
              UITextView() 
          }
          func updateUIView(_ uiView: UITextView, context: Context) {
          }
      }
      #endif
    • 13:33 - ContainerView with TextView and line number view

      // Create a text view subclass for a code editor
      
      import UIKit
      
      class TextView: UITextView {}
      
      class ContainerView: UIView {
          let textView = TextView()
          let lineNumberView = UIView()
         
          textView.font = UIFont.monospacedSystemFont
      }
    • 14:42 - Three NSTextViewportLayoutControllerDelegate overrides

      // Override viewport controller delegate methods
      
      class TextView: UITextView {
          // Set up
      		override func textViewportLayoutControllerWillLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
          	super.textViewportLayoutControllerWillLayout(textViewportLayoutController)
            //...
          }
      
          // Get paragraph bounds
          override func textViewportLayoutController (_ textViewportLayoutController: NSTextViewportLayoutController, configureRenderingSurfaceFor textLayoutFragment: NSTextLayoutFragment) {
      			super.textViewportLayoutController(textViewportLayoutController, configureRenderingSurfaceFor: textLayoutFragment)
            //...
          }
      
          // Share accumulated info back to ContainerView
      		override func textViewportLayoutControllerDidLayout (_ textViewportLayoutController: NSTextViewportLayoutController) {
      		  super.textViewportLayoutControllerDidLayout(textViewportLayoutController)
            //...
          }
      }
    • 15:59 - startingLineNumber(for:) using enumerateTextElements

      func startingLineNumber(for viewportRange: NSTextRange?) -> Int {
          guard let viewportRange,
                let storage = textLayoutManager?.textContentManager
                    as? NSTextContentStorage else { return 0 }
          let startLocation = storage.documentRange.location
          var count = 1
          storage.enumerateTextElements(from: startLocation) { element in
              guard let range = element.elementRange else { return true }
              if range.location.compare(viewportRange.location)
                  != .orderedAscending { return false }
              count += 1
              return true
          }
          return count
      }
    • 17:02 - DidLayout: convert frames to viewport coordinates

      // Override viewport controller delegate methods
      
      class TextView: UITextView {
          private var lines: [CGRect] = []
          private var startingLineNumber = 0
          var onDidLayout: ((Int, [CGRect]) -> Void)?
      
          // Share accumulated info back to ContainerView
      		override func textViewportLayoutControllerDidLayout (_ textViewportLayoutController: NSTextViewportLayoutController) {
              super.textViewportLayoutControllerDidLayout(controller)
              let origin = controller.viewportBounds.origin
              onDidLayout?(startingLineNumber, lines.map {$0.offsetBy(dx: 0, dy: -origin.y) })
          }
      }
    • 17:16 - Draw line numbers in ContainerView closure

      // Draw line numbers in the ContainerView
      
      class ContainerView: UIView {
          let textView = TextView()
          let lineNumberView = UIView()
          func setup() {
              textView.onDidLayout = {startingLineNumber, lines in
                  let attributes: [NSAttributedString.Key: Any] = [
                      .font: UIFont.monospacedSystemFont(ofSize: 11, weight: .regular),
                      .foregroundColor: UIColor.secondaryLabel
                  ]
                  for (i, frame) in lines.enumerated() {
                      let number = "\(startingLineNumber + i)" as NSString
                      number.draw(at: CGPoint(x: 8, y: frame.minY),
                          withAttributes: attributes)
                  }
              }
          }
      }
    • 19:22 - Collapsible sections: full TextView class

      // Add collapsible sections to your text view
      
      class TextView: UITextView, NSTextContentStorageDelegate {
          var collapsedSections: Set<Int> = []
      
          // Set up
      		override func textViewportLayoutControllerWillLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
          	super.textViewportLayoutControllerWillLayout(textViewportLayoutController)
            //...
          }
      
          // Get paragraph bounds
          override func textViewportLayoutController (_ textViewportLayoutController: NSTextViewportLayoutController, configureRenderingSurfaceFor textLayoutFragment: NSTextLayoutFragment) {
      			super.textViewportLayoutController(textViewportLayoutController, configureRenderingSurfaceFor: textLayoutFragment)
            //...
          }
      
          // Share accumulated info back to ContainerView
      		override func textViewportLayoutControllerDidLayout (_ textViewportLayoutController: NSTextViewportLayoutController) {
      		  super.textViewportLayoutControllerDidLayout(textViewportLayoutController)
            //...
          }
        
          // Skip layout for paragraphs marked as collapsed
          func textContentManager(shouldEnumerate textElement: NSTextElement, options: NSTextContentManager.EnumerationOptions) -> Bool {
            //...
          }
      
          // Handle section collapse toggling
          func toggleSection(headerOffset: Int) {
              if collapsedSections.contains(headerOffset) {
                  collapsedSections.remove(headerOffset)
              } else {
                  collapsedSections.insert(headerOffset)
              }
              guard let textLayoutManager = textLayoutManager else { return }
      
              let textViewportLayoutController = textLayoutManager.textViewportLayoutController
              textViewportLayoutController.delegate?.textViewportLayoutControllerReceivedSetNeedsLayout?(textViewportLayoutController)
          }
      }
    • 22:06 - Text attachment view provider reuse policy

      // Cache text attachment view providers
      
      import UIKit
      
      class ViewController: UIViewController {
      
          var textView: UITextView
          
          func setupTextView() {
              textView = UITextView()
              textView.register(
                  [.onEditingInlineParagraphs],
                  forTextAttachmentViewProviderType: AnimatedAttachmentViewProvider.self
              )
          }
      }
    • 0:00 - Introduction
    • TextKit and the tension between using framework text views versus building custom ones. TextKit is Apple's text engine powering text controls in SwiftUI, UIKit, and AppKit.

    • 3:09 - TextKit architecture
    • Walk through TextKit's four-layer architecture: text storage, layout, viewport, and view. See how NSTextContentStorage breaks an attributed string into NSTextParagraph elements, how NSTextLayoutManager produces immutable NSTextLayoutFragments, and how NSTextViewportLayoutController coordinates with the text view to efficiently render only the paragraphs visible in the viewport.

    • 9:17 - What's new in TextKit
    • Meet the new NSTextViewportRenderingSurface protocol — a common abstraction for views or layers that draw layout fragments — and NSTextViewportRenderingSurfaceKey, which uniquely identifies surfaces across viewport layout cycles Use the new delegate methods to assign and query rendering surfaces during the viewport layout process.

    • 11:27 - Extending framework text views
    • UITextView and NSTextView now publicly conform to NSTextViewportLayoutControllerDelegate, so you can subclass and override willLayout, configureRenderingSurface, and didLayout to extend their behavior. Use a SwiftUI ViewRepresentable to bring these text views into a SwiftUI app.

    • 12:58 - Example: Code editor with line numbers
    • Build a code-editor experience by subclassing UITextView and overriding the viewport controller delegate methods. Calculate the starting line number with enumerateTextElements, capture each layout fragment's bounds in configureRenderingSurface, and pass the results to a container view that draws line numbers alongside the text.

    • 17:52 - Example: Collapsible recipe sections
    • Modify layout for multiple paragraphs by conforming to NSTextContentStorageDelegate. Use textContentManager(_:shouldEnumerate:) to skip layout for collapsed paragraphs, track collapsed paragraph offsets in state, and toggle them in response to user taps — collapsing each multi-paragraph recipe down to just its heading.

    • 19:56 - Text attachments and view provider reuse
    • Text attachments use the same TextKit architecture as regular text, with NSTextAttachmentViewProvider supplying the view. New in 2027: register a reuse policy with UITextView using register(_:forTextAttachmentViewProviderType:). Use onEditingInlineParagraphs to preserve view providers across edits and onScrollingOutOfViewport to cache surfaces when they scroll off screen.

    • 23:00 - Next steps
    • Kickstart your app with UITextView, NSTextView, or TextEditor; extend them via the viewport controller delegate hooks; or use TextKit directly to build fully custom rendering. Download the sample app to explore each example.

Developer Footer

  • 视频
  • WWDC26
  • 使用 TextKit 提升 App 的文本体验
  • 打开菜单 关闭菜单
    • iOS
    • iPadOS
    • macOS
    • Apple tvOS
    • visionOS
    • watchOS
    打开菜单 关闭菜单
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • SF Symbols
    打开菜单 关闭菜单
    • 辅助功能
    • 配件
    • Apple 智能
    • App 扩展
    • App Store
    • 音频与视频 (英文)
    • 增强现实
    • 设计
    • 分发
    • 教育
    • 字体 (英文)
    • 游戏
    • 健康与健身
    • App 内购买项目
    • 本地化
    • 地图与位置
    • 机器学习与 AI
    • 开源资源 (英文)
    • 安全性
    • Safari 浏览器与网页 (英文)
    打开菜单 关闭菜单
    • 完整文档 (英文)
    • 部分主题文档 (简体中文)
    • 教程
    • 下载
    • 论坛 (英文)
    • 视频
    打开菜单 关闭菜单
    • 支持文档
    • 联系我们
    • 错误报告
    • 系统状态 (英文)
    打开菜单 关闭菜单
    • Apple 开发者
    • App Store Connect
    • 证书、标识符和描述文件 (英文)
    • 反馈助理
    打开菜单 关闭菜单
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program (英文)
    • Mini Apps Partner Program
    • News Partner Program (英文)
    • Video Partner Program (英文)
    • 安全赏金计划 (英文)
    • Security Research Device Program (英文)
    打开菜单 关闭菜单
    • 与 Apple 会面交流
    • Apple Developer Center
    • App Store 大奖 (英文)
    • Apple 设计大奖
    • Apple Developer Academies (英文)
    • WWDC
    阅读最近新闻。
    获取 Apple Developer App。
    版权所有 © 2026 Apple Inc. 保留所有权利。
    使用条款 隐私政策 协议和准则