# dotNet.ReoGrid 扩展库 - 富文本格式

富文本格式由 `unvell.ReoGrid.Drawing.RichText` 类表示，可以添加到单元格或浮动对象中显示和打印。

## 在单元格中显示富文本

![308](../images/308.png)

aardio 示例代码：

```aardio
import dotNet.ReoGrid;
var sheet1 = grid.CurrentWorksheet;
sheet1.MergeRange("B2:F6");

// 创建富文本对象并设置样式
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Regular("The ")
    .Bold("Rich Text Format")
    .Regular(" (often abbreviated ")
    .Bold("RTF")
    .Regular(") is a proprietary")
    .Superscript("[6][7][8]")
    .Regular(" document file format with published specification developed by Microsoft Corporation from ")
    .Span("1987", { textColor = ReoGrid.Graphics.SolidColor.OrangeRed })
    .Span(" until ", { textColor = ReoGrid.Graphics.SolidColor.Black })
    .Span("2008", { textColor = ReoGrid.Graphics.SolidColor.OrangeRed })
    .Span(" for cross-platform document interchange with Microsoft products.", { textColor = ReoGrid.Graphics.SolidColor.Black });
```

## 换行处理

设置单元格样式实现自动换行或强制换行。

示例：不换行的文本  
![311](../images/311.png)

示例：自动换行的文本  
![312](../images/312.png)

aardio 代码：

```aardio
// 设置单元格文本自动换行
sheet1.Cells["B2"].Style.TextWrap = ReoGrid.TextWrapMode.WordBreak;
```

## 文本对齐

通过设置单元格的水平对齐和垂直对齐样式来改变富文本的对齐方式。

![313](../images/313.png)

aardio 代码：

```aardio
// 垂直居中
sheet1.Cells["B2"].Style.VAlign = ReoGrid.ReoGridVerAlign.Middle;
```

设置右对齐：  
![314](../images/314.png)

aardio 代码：

```aardio
// 水平右对齐
sheet1.Cells["B2"].Style.HAlign = ReoGrid.ReoGridHorAlign.Right;
```

## ReoGrid 富文本结构

### 最简单的文本

![309](../images/309.png)

使用 `Span` 方法添加无特殊样式的文本：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Span("The Apple II (styled as apple ][) is an 8-bit home computer.");
```

### 加粗文本

![310](../images/310.png)

aardio 代码：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Span("The ")
    .Bold("Apple II")
    .Regular(" (styled as apple ][) is an 8-bit home computer.");
```

使用 `Bold` 方法加粗文本，`Regular` 方法恢复默认样式。斜体、下划线、删除线等样式的使用方法与加粗类似。

### 添加段落（换行）

使用 `NewLine` 方法换行：  
![315](../images/315.png)

aardio 代码：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Span("The ")
    .Bold("Apple II")
    .Regular(" (styled as apple ][) is an 8-bit home computer.")
    .NewLine()
    .Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.");
```

### 段落间距和行高

`NewLine` 方法会创建新段落：  
![316](../images/316.png)

可以调整段落间距和行高：  
![317](../images/317.png)

使用 `SetStyles` 方法修改当前段落或文本段的样式：  
![318](../images/318.png)

aardio 代码：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .SetStyles({ paragraphSpacing = 3.0 })
    .Span("The ")
    .Bold("Apple II")
    .Regular(" (styled as apple ][) is an 8-bit home computer.")
    .NewLine()
    .SetStyles({ lineHeight = 2.0 })
    .Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.");
```

注意：Excel 不支持调整段落间距和行高样式。

### 每段独立水平对齐

![319](../images/319.png)

aardio 代码：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Span("The ")
    .Bold("Apple II")
    .Regular(" (styled as apple ][) is an 8-bit home computer.")
    .NewLine()
    .Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.")
    .NewLine()
    .SetStyles({ halign = ReoGrid.ReoGridHorAlign.Right })
    .Span("- wikipedia.org");
```

注意：Excel 不支持每段独立设置对齐方式。

### 上标和下标

使用 `Superscript` 方法创建上标：  
![320](../images/320.png)

aardio 代码：

```aardio
sheet1["B2"] = ReoGrid.Drawing.RichText()
    .Span("The ")
    .Bold("Apple II")
    .Regular(" (styled as apple ][) is an 8-bit home computer.")
    .NewLine()
    .Span("One of the first highly successful mass-produced microcomputer products,")
    .Superscript("[2]")
    .Regular(" designed primarily by Steve Wozniak.")
    .NewLine()
    .SetStyles({ halign = ReoGrid.ReoGridHorAlign.Right })
    .Span("- wikipedia.org");
```

## 在绘图对象中显示富文本

要在绘图对象中显示富文本，创建 `RichText` 实例并添加到绘图对象中。

## Excel 导入/导出支持

ReoGrid 支持从 Excel 文件导入和导出富文本格式。下图是在 ReoGrid 和 Excel 中显示的相同文本：  
![321](../images/321.png)