officefileapi-devexpress-dot-spreadsheet-dot-alignment-a45e9ce2.md
Specifies whether text size in a cell changes automatically to fit the cell width.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
bool ShrinkToFit { get; set; }
Property ShrinkToFit As Boolean
| Type | Description |
|---|---|
| Boolean |
true to automatically change text size to fit a cell; otherwise, false.
|
If the ShrinkToFit property is enabled, the size of cell text changes as follows:
Note
The ShrinkToFit option does not change the Size property value of the cell font.
If cell text wraps into multiple lines (WrapText is true ), the ShrinkToFit option does not apply.
The Spreadsheet offers you an alternative method to make sure a cell displays its content completely. Call the Column.AutoFit method to increase the column width instead of reducing text size.
The code sample below demonstrates how to specify the ShrinkToFit option for an individual cell. Use the cell’s Formatting.Alignment property to access and modify the Alignment object’s properties.
var cellB1 = worksheet.Cells["B1"];
cellB1.Value = "The Alignment.ShrinkToFit property is applied";
cellB1.Alignment.ShrinkToFit = true;
Dim cellB1 As Cell = worksheet.Cells("B1")
cellB1.Value = "The Alignment.ShrinkToFit property is applied"
cellB1.Alignment.ShrinkToFit = True
To specify alignment options for a cell range, modify the Alignment object’s properties within the CellRange.BeginUpdateFormatting—CellRange.EndUpdateFormatting method pair.
var rangeA1C3 = worksheet.Range["A1:C3"];
var rangeFormatting = rangeA1C3.BeginUpdateFormatting();
rangeFormatting.Alignment.ShrinkToFit = true;
rangeA1C3.EndUpdateFormatting(rangeFormatting);
Dim rangeA1C3 As CellRange = worksheet.Range("A1:C3")
Dim rangeFormatting As Formatting = rangeA1C3.BeginUpdateFormatting()
rangeFormatting.Alignment.ShrinkToFit = True
rangeA1C3.EndUpdateFormatting(rangeFormatting)
You can also use cell styles to share the same alignment options between multiple cells. Refer to the following help topic for details on how to specify custom format settings for a cell style: How to: Create or Modify a Style.
Select a cell or cell range to which you want to apply the ShrinkToFit option. On the Home tab, click the dialog box launcher in the bottom-right corner of the Alignment group to invoke the Format Cells dialog.
Select the Shrink to fit checkbox to enable this option for selected cells.
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ShrinkToFit property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
cellB1.Value = "The Alignment.ShrinkToFit property is applied";
cellB1.Alignment.ShrinkToFit = true;
cellB1.Value = "The Alignment.ShrinkToFit property is applied"
cellB1.Alignment.ShrinkToFit = True
' Align the "B2" cell content.
See Also
How to: Format a Cell or Range of Cells
How to: Align Cell Content in Spreadsheet Documents