办公效率

SwiftUI富文本编辑器

为应用实现混合样式文本展示、Markdown 渲染,以及支持选区格式调整和工具栏的编辑器。

# 样式文本编辑

在 SwiftUI 中显示样式文本和构建富文本编辑器的模式。涵盖 Text 样式、AttributedString、使用 TextEditor 基于选区设置格式,以及自定义格式定义。

## 此 Skill 的适用场景

当用户有以下需求时,使用此 Skill:
- 想要显示带样式或格式的文本
- 需要带有粗体、斜体、下划线控件的富文本编辑功能
- 询问 AttributedString 或 TextEditor
- 想要文本格式工具栏
- 询问 Text 视图中的 Markdown 渲染
- 需要自定义文本属性或格式约束
- 想要基于选区设置文本格式

## 决策树

```
What text feature do you need?
|
+- Display styled read-only text
|  +- Simple styling (one style) -> Text Styling section below
|  +- Mixed styles in one string -> AttributedString section below
|  +- Markdown content -> Markdown section below
|
+- Edit plain text
|  +- TextEditor(text: $stringBinding)
|
+- Edit rich/styled text
|  +- Basic rich editor -> Rich Text Editing section below
|  +- With formatting toolbar -> Formatting Controls section below
|  +- With custom constraints -> Custom Formatting section below
```

## API 可用性

| API | 最低版本 | 备注 |
|-----|----------------|-------|
| 带修饰符的 `Text` | iOS 13 | .font(), .bold(), .italic() |
| `TextEditor(text:)` | iOS 14 | 纯字符串绑定 |
| `.foregroundStyle()` | iOS 15 | 优先于 .foregroundColor() 使用 |
| `AttributedString` | iOS 15 | 富文本模型 |
| `.bold(condition)` | iOS 16 | 条件式粗体/斜体 |
| `.underline(pattern:)` | iOS 16 | 虚线、点线样式 |
| `TextEditor(text:selection:)` | iOS 18 | AttributedString + 选区 |
| `AttributedTextSelection` | iOS 18 | 基于选区设置格式 |
| `text.transformAttributes(in:)` | iOS 18 | 修改选区内的属性 |
| `AttributedTextFormattingDefinition` | iOS 18 | 自定义格式约束 |
| `@Environment(\.fontResolutionContext)` | iOS 18 | 解析字体属性 |

## 5 个最常见的错误

| # | 错误 | 修复方法 |
|---|---------|-----|
| 1 | 在新项目中使用 `.foregroundColor()` | 使用 `.foregroundStyle()` — 支持渐变和 ShapeStyle |
| 2 | 在文本上使用 `.animation(.spring())` 时未提供 `value:` | 在 iOS 15 中已弃用 — 始终传入 `value:` 参数 |
| 3 | 期望 `Text` 支持完整的 Markdown | Text 仅支持行内 Markdown:**粗体**、*斜体*、[链接]。不支持列表、表格、代码块、图片 |
| 4 | 每次计算 body 时都创建新的 AttributedString 实例 | 将复杂的 AttributedString 对象缓存在 @State 或 body 外的计算属性中 |
| 5 | 使用 `TextEditor(text: $string)` 处理富文本 | 使用带 AttributedString 绑定的 `TextEditor(text: $attributedString, selection: $selection)`(iOS 18+)|

## Text 样式速查

```swift
// Font
Text("Hello").font(.headline)
Text("Custom").font(.system(size: 24, design: .rounded))

// Weight
Text("Bold").fontWeight(.bold)

// Color — prefer foregroundStyle over foregroundColor
Text("Styled").foregroundStyle(.red)
Text("Gradient").foregroundStyle(
    .linearGradient(colors: [.yellow, .blue], startPoint: .top, endPoint: .bottom)
)

// Decorations
Text("Bold").bold()
Text("Conditional").bold(someCondition)
Text("Underline").underline(true, pattern: .dash, color: .blue)
Text("Strike").strikethrough(true, pattern: .dot, color: .red)

// Layout
Text("Centered").multilineTextAlignment(.center)
Text("Spaced").lineSpacing(10)
Text("Truncated").lineLimit(2).truncationMode(.tail)
```

## AttributedString

```swift
// Create and style ranges
var text = AttributedString("Red and Blue")
if let redRange = text.range(of: "Red") {
    text[redRange].foregroundColor = .red
    text[redRange].font = .headline
}
if let blueRange = text.range(of: "Blue") {
    text[blueRange].foregroundColor = .blue
    text[blueRange].underlineStyle = .single
}

// Display
Text(text)
```

### 可用属性

| 属性 | 类型 | 示例 |
|-----------|------|---------|
| `.font` | Font | `.headline` |
| `.foregroundColor` | Color | `.red` |
| `.backgroundColor` | Color | `.yellow` |
| `.underlineStyle` | UnderlineStyle | `.single` |
| `.underlineColor` | Color | `.blue` |
| `.strikethroughStyle` | UnderlineStyle | `.single` |
| `.strikethroughColor` | Color | `.red` |
| `.inlinePresentationIntent` | InlinePresentationIntent | `.stronglyEmphasized`(粗体)、`.emphasized`(斜体)|

## 富文本编辑(iOS 18+)

```swift
struct RichTextEditorView: View {
    @State private var text = AttributedString("Select text to format")
    @State private var selection = AttributedTextSelection()

    @Environment(\.fontResolutionContext) private var fontResolutionContext

    var body: some View {
        VStack {
            TextEditor(text: $text, selection: $selection)
                .frame(height: 200)

            HStack {
                Button(action: toggleBold) {
                    Image(systemName: "bold")
                }
                Button(action: toggleItalic) {
                    Image(systemName: "italic")
                }
                Button(action: toggleUnderline) {
                    Image(systemName: "underline")
                }
            }
        }
    }

    private func toggleBold() {
        text.transformAttributes(in: &selection) {
            let font = $0.font ?? .default
            let resolved = font.resolve(in: fontResolutionContext)
            $0.font = font.bold(!resolved.isBold)
        }
    }

    private func toggleItalic() {
        text.transformAttributes(in: &selection) {
            let font = $0.font ?? .default
            let resolved = font.resolve(in: fontResolutionContext)
            $0.font = font.italic(!resolved.isItalic)
        }
    }

    private func toggleUnderline() {
        text.transformAttributes(in: &selection) {
            if $0.underlineStyle != nil {
                $0.underlineStyle = nil
            } else {
                $0.underlineStyle = .single
            }
        }
    }
}
```

### 读取选区属性

```swift
// Get current attributes at the selection/cursor
let attributes = selection.typingAttributes(in: text)
let currentColor = attributes.foregroundColor ?? .primary
```

## 自定义格式定义(iOS 18+)

限制编辑器中可用的格式选项:

```swift
struct MyTextFormatting: AttributedTextFormattingDefinition {
    typealias Scope = AttributeScopes.SwiftUIAttributes

    static let fontWeight = ValueConstraint(
        \.font,
        constraint: { font in
            guard let font = font else { return nil }
            let weight = font.resolve().weight
            return font.weight(weight == .bold ? .regular : .bold)
        }
    )
}

// Apply to TextEditor
TextEditor(text: $text, selection: $selection)
    .textFormattingDefinition(MyTextFormatting.self)
```

## Text 中的 Markdown

```swift
// Inline markdown support
Text("This is **bold** and *italic* text")
Text("Visit [Apple](https://www.apple.com)")
Text("Code: `inline code`")
```

### Markdown 的限制

Text 仅支持**行内** Markdown:
- ✅ 粗体(`**text**`)、斜体(`*text*`)、链接、行内代码
- ❌ 换行、块级格式、列表、表格、代码块、图片、块引用

## 审查清单

### 文本显示
- [ ] 使用 `.foregroundStyle()`,而非已弃用的 `.foregroundColor()`
- [ ] 对截断的文本,将 `.lineLimit()` 与 `.help()` 工具提示或 `.textSelection(.enabled)` 配合使用
- [ ] 支持 Dynamic Type — 使用系统字体样式,而非硬编码字号
- [ ] 颜色适配深色模式 — 使用系统颜色或语义样式

### AttributedString
- [ ] 将复杂的 AttributedString 缓存在 @State 中,而非在 body 中重新创建
- [ ] 范围查找使用安全的 `if let range = text.range(of:)` 模式
- [ ] 使用适当的属性(语义化粗体/斜体使用 `.inlinePresentationIntent`)

### TextEditor
- [ ] 富文本使用 `TextEditor(text: $attributedString, selection: $selection)`(iOS 18+)
- [ ] 格式工具栏使用 `text.transformAttributes(in: &selection)` 模式
- [ ] 字体解析使用 `@Environment(\.fontResolutionContext)` 实现切换逻辑
- [ ] 为格式按钮提供无障碍标签

### 本地化
- [ ] 面向用户的字符串使用 `LocalizedStringKey`
- [ ] 本地化字符串中的 Markdown 使用 `Text(LocalizedStringKey("**Bold** text"))`

## 参考资料

- [SwiftUI Text 和 Symbol 修饰符](https://developer.apple.com/documentation/SwiftUI/View-Text-and-Symbols)
- [构建丰富的 SwiftUI 文本体验](https://developer.apple.com/documentation/SwiftUI/building-rich-swiftui-text-experiences)
- [AttributedString 文档](https://developer.apple.com/documentation/Foundation/AttributedString)
- [TextEditor 文档](https://developer.apple.com/documentation/SwiftUI/TextEditor)

同类提示词

办公效率· n8n 工作流的对话模型

商品照片转棚拍广告生图指令

上传商品照片并提供说明文字,分析材质、配色和原图问题,写出保留商品外观的棚拍广告生图指令。

=你是一位专业产品摄影师和广告创意总监。分析上传的产品图片及其配文:{{ $('Telegram Trigger Received').item.json.message.caption }} 你的任务是创建一段详细的图像生成提示词,将这张产品照片转化为高端、商业级的广告视觉作品。 分析要求: 1. **产品识别**:识别产品类型、尺寸、类别、材质、配色和关键特征 2. **当前状态评估**:指出当前的光线、背景、构图和质量问题 3. **目标美学**:根据产品类别确定理想的广告风格(奢侈品、科技、食品、时尚等) 输出(严格使用 JSON 格式):
办公效率· n8n 工作流的对话模型

Veo 3营销视频分镜指令

填写视频需求、营销目标、图像描述和画幅,生成包含场景、运镜、动作、灯光与结尾的结构化制片指令。

=# Veo3 视频生成系统提示词 你是一名专业的视频提示词工程师,擅长为 Veo3 视频生成撰写细致、富有电影感的提示词。你的角色是分析用户输入,将其转化为全面的视频生成指令,以产出高质量、引人入胜的内容。 ## 输入分析 你将收到以下用户输入: - **AI Videos instructions**:视频生成的核心用户请求 - **Campaign objectives**:营销活动目标和说明 - **Images Description**:用户提供的视觉背景信息 - **Aspect Ratio**:要求的视频尺寸比例 ## 输出格式 你
可填 21 项
办公效率· n8n 工作流的对话模型

保留原品的社媒广告视觉方案

提供商品图和配文,生成保留原品外观的广告生图说明,广告文字仅从配文提取,缺项留空。

={ "system_prompt": "你是一位顶尖创意总监,专攻爆款产品广告,拥有15年以上奢侈品牌广告活动经验。将产品图片转化为商业级广告,让浏览者停下滑动并促进转化。", "instructions": { "core_rules": [ "保持原始产品100%不变——不得修改、替换或扭曲", "只提取和使用用户配文中提供的文字", "如果配文中没有相关文字,文字字段留空——绝不生成AI广告语", "专注于高端视觉强化和商业摄影技巧", "制作适合在Instagram展
办公效率· Claude Fable 5.2

用代码制作长纪录片流程

从题材调研、脚本和样帧审核开始,按章节生成三维分镜、旁白、环境声与可复现渲染。

<inputs> 向我询问: 1. 我的主题 2. 目标视频时长 3. 我是否已有脚本 如果我只给你一个主题,先研究该主题并撰写脚本。 开始制作任何内容之前,先向我展示: - 完整脚本 - 章节拆分 - 前 60 seconds 的分镜计划 - 6 张展示预期视觉方向的静帧 在我认可视觉效果之前,不要渲染完整视频。 </inputs> <story> 按照高品质 YouTube 纪录片的方式来写,不要写成 AI 科普解说。 故事必须始终向前推进。 每个部分都需要包含以下内容之一: - 一个新问题 - 一个揭示 - 一个矛盾 - 一个后果 - 一条证据
办公效率

同场景表情变化九宫格自拍

上传主角照片,可选搭档参考图或设定,并填写服装、背景或姿势偏好,生成同一布光和构图的九张变化。

# photo-booth ## 何时使用此 skill - 当用户希望根据参考图制作大头贴宫格、表情宫格、情侣自拍宫格、同步姿势组图或多种人像变体时使用。 - 当请求制作 3x3 宫格,且主体、房间、服装、构图和布光保持一致,仅表情或细微姿势发生变化时触发。 ## 必需输入 - 主要人物的主参考图。 - 可选的伴侣或角色参考图。 - 如果未提供伴侣参考图,需提供期望的伴侣设定。 - 用户指定的服装、背景、摄影时代、姿势清单或氛围。 如果缺少主参考图,请用户附上。如果伴侣设定或预期风格不明确,提出一个有针对性的问题。 ## 连续性规则 -
需参考图
办公效率· Claude Code / 动效编程

单镜头立体落地页飞越动画

提供落地页链接、品牌名、标志和两段收尾文案,可选配乐,制作卡片随镜头掠过而浮起的连续飞越短片。

<inputs> 向我索取:我的落地页链接、品牌名称和标志、由两部分组成的结尾文案(一句陈述,后接一小段斜体文字),以及一条 15 second 音轨(可选)。 </inputs> <direction> 以 60fps 对我的落地页进行 14.5 second 3D 飞越拍摄,1440x1152。页面平放在明亮的摄影棚地面上(中央为 #f9fbfb,角落为 #f2f3f6),周围有柔和宽阔的阴影,圆角为 24px。全程使用一个连续镜头,绝不切镜:镜头从首屏上方低处开始,以恒定速度沿页面向后滑行,同时朝页面上方看去;然后垂直升至正上方,将整个页面呈现为
✓