# 圖案與文字

- Human documentation: [https://docs.univer.ai/zh-TW/guides/slides/features/core/shapes-and-text](https://docs.univer.ai/zh-TW/guides/slides/features/core/shapes-and-text)

- Agent Markdown: [https://docs.univer.ai/zh-TW/guides/slides/features/core/shapes-and-text.md](https://docs.univer.ai/zh-TW/guides/slides/features/core/shapes-and-text.md)

- Requested language: `zh-TW`

- Content language: `zh-TW`

- Documentation version: `1.2.0`

- Source: [slides/features/core/shapes-and-text.zh-TW.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/slides/features/core/shapes-and-text.zh-TW.mdx)

---

Slides 使用共用 Shape 引擎提供可編輯圖案、連接線和圖案文字。`FSlide.insertShape()` 傳回由 Slides 命令管線驅動的圖案 Facade。

## 插入圖案

```ts
const slide = univerAPI.getActivePresentation()?.getActiveSlide()

if (!slide) {
  throw new Error('No active slide')
}

const shape = slide.insertShape({
  shapeType: univerAPI.Enum.ShapeTypeEnum.RoundRect,
  transform: { left: 80, top: 64, width: 320, height: 120 },
  visible: true,
  selectable: true,
  shapeData: {
    fill: {
      fillType: univerAPI.Enum.ShapeFillEnum.SolidFill,
      color: '#dbeafe',
    },
  },
})
```

Shape 宿主配接器無法使用或輸入無法建立時，`insertShape()` 可能傳回 `null`。插入圖案前，請註冊 Slides 設定所需的 Shape 相依套件。

## 新增富文字

```ts
if (shape) {
  const richText = univerAPI.newRichText()
    .text('Quarterly ')
    .bold('review')

  shape.getText()
    .setRichText(richText)
    .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER)
    .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)
}
```

使用 `slide.getShape(id)` 或 `slide.getShapes()` 取得圖案。通用元素方法可更新位置、尺寸、可見性、名稱和描述；`slide.deleteElement(shape)` 會刪除圖案。

## 編輯形狀文字

使用下方或前面範例中 `insertShape()` 回傳的形狀物件。`getText()` 回傳即時的 [FShapeText](https://docs.univer.ai/zh-TW/reference/facade/shape-text.md) 物件，可透過 `getPlainText()` 讀取純文字、`setText()` 取代全部文字，並設定字型、顏色與對齊方式。

```ts
if (!shape) throw new Error('Shape is not available')
const shapeText = shape.getText()
console.log(shapeText.getPlainText())
shapeText
  .setText('Quarterly review')
  .setFontSize(18)
  .setColor('#2563eb')
  .setBold(true)
  .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER)
  .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)
```

`setText()` 會取代既有文字；同一形狀需要多種文字樣式時，請使用 `setRichText()`。編輯既有豐富文字時，先呼叫 `getRichText()?.copy()`，修改建構器後再傳給 `setRichText()`；僅修改建構器不會自動寫回。更多選項請參閱[文字樣式](https://docs.univer.ai/zh-TW/reference/facade/shape-text.md#settextstyle)與[文字方塊內距和版面配置](https://docs.univer.ai/zh-TW/reference/facade/shape-text.md#settextboxoptions)。

```ts
const richText = univerAPI.newRichText()
  .text('Quarterly ')
  .bold('review')
shapeText.setRichText(richText)
```

## 圖案格式刷

選取來源圖案，啟用格式刷，再選取目標。格式刷複製填滿、外框、效果與支援的文字格式，保留目標識別、幾何、文字內容及公式繫結。清除格式會還原插入時的預設樣式。SmartArt 不適用；對應的圖案 UI 外掛會自動註冊此行為。
