SuperAPI AI 工具集

axiom-textkit-ref

写作与研究

查看项目

更新于 10-16 00:23

技能介绍

用途:这是面向 Apple 平台开发者的 TextKit 2 参考资料,讲解文本渲染架构、从 TextKit 1 迁移、Writing Tools 集成,以及 SwiftUI TextEditor 配合 AttributedString 使用,覆盖至 iOS 26。

架构:TextKit 2 采用 MVC 模式。模型层含 NSTextContentManager(抽象类,默认实现 NSTextContentStorage)生成 NSTextElement(默认 NSTextParagraph);控制器层 NSTextLayoutManager 替代 NSLayoutManager,不再提供字形 API,产出 NSTextLayoutFragment 与 NSTextLineFragment;视图层由 NSTextViewportLayoutController 负责可见区域布局,NSTextContainer 提供几何信息与排除路径。文本范围改用对象化的 NSTextLocation、NSTextRange、NSTextSelection。

核心原则:不暴露字形 API,以兼容国际文字(如卡纳达语、阿拉伯语中字符与字形非一一对应);元素、布局片段、选择等采用不可变值语义;布局始终为非连续,仅处理可见区域加 overscroll 区。

迁移:需从 NSRange 转为 NSTextLocation/NSTextRange(可借助内容管理器的 location/offset 方法互转),字形 API 没有直接替代,需改用 enumerateTextLayoutFragments 等高层枚举。访问 textView.layoutManager 会触发回退到 TextKit 1 且不可逆、丢失 UI 状态,应先检查 textLayoutManager。可用 UITextView(usingTextLayoutManager: true)、NSTextView(usingTextLayoutManager: true) 显式启用。

Writing Tools:UITextView/NSTextView 必须用 TextKit 2 才能获得完整体验,否则仅面板显示改写结果。可通过 writingToolsBehavior、writingToolsResultOptions、writingToolsIgnoredRangesIn 等控制行为与保护范围;iOS 26 起为自定义文本引擎提供 UIWritingToolsCoordinator/NSWritingToolsCoordinator 与对应 delegate。

限制与其他事实:文档给出委托钩子、自定义布局片段、渲染属性、附视图、列表与命中测试等模式;具体价格、平台版本之外的收费与效果未在资料中提供。