• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Render Fit
2
3The **renderFit** attribute sets how the final state of a component's content is rendered during its width and height animation process.
4
5>  **NOTE**
6>
7>  The APIs of this module are supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
8
9## renderFit
10
11renderFit(fitMode: RenderFit)
12
13Sets how the final state of the component's content is rendered during its width and height animation process.
14
15**Atomic service API**: This API can be used in atomic services since API version 11.
16
17**System capability**: SystemCapability.ArkUI.ArkUI.Full
18
19**Parameters**
20
21| Name | Type                           | Mandatory| Description                                                        |
22| ------- | ------------------------------- | ---- | ------------------------------------------------------------ |
23| fitMode | [RenderFit](#renderfit) | Yes  | How the final state of the component's content is rendered during its width and height animation process.<br>If **renderFit** is not set, the default value **RenderFit.TOP_LEFT** is used.|
24
25## RenderFit
26
27**Atomic service API**: This API can be used in atomic services since API version 11.
28
29| Name                         | Description                                      | Diagram                                     |
30| --------------------------- | ---------------------------------------- | ---------------------------------------- |
31| CENTER                      | The component's content stays at the final size and always aligned with the center of the component.            | ![renderfit_center](figures/renderfit_center.png) |
32| TOP                         | The component's content stays at the final size and always aligned with the top center of the component.          | ![renderfit_top](figures/renderfit_top.png) |
33| BOTTOM                      | The component's content stays at the final size and always aligned with the bottom center of the component.          | ![renderfit_bottom](figures/renderfit_bottom.png) |
34| LEFT                        | The component's content stays at the final size and always aligned with the left of the component.            | ![renderfit_left](figures/renderfit_left.png) |
35| RIGHT                       | The component's content stays at the final size and always aligned with the right of the component.            | ![renderfit_right](figures/renderfit_right.png) |
36| TOP_LEFT                    | The component's content stays at the final size and always aligned with the upper left corner of the component.           | ![renderfit_top_left](figures/renderfit_top_left.png) |
37| TOP_RIGHT                   | The component's content stays at the final size and always aligned with the upper right corner of the component.           | ![renderfit_top_right](figures/renderfit_top_right.png) |
38| BOTTOM_LEFT                 | The component's content stays at the final size and always aligned with the lower left corner of the component.           | ![renderfit_bottom_left](figures/renderfit_bottom_left.png) |
39| BOTTOM_RIGHT                | The component's content stays at the final size and always aligned with the lower right corner of the component.           | ![renderfit_bottom_right](figures/renderfit_bottom_right.png) |
40| RESIZE_FILL                 | The component's content is always resized to fill the component's content box, without considering its aspect ratio in the final state.           | ![renderfit_resize_fill](figures/renderfit_resize_fill.png) |
41| RESIZE_CONTAIN              | While maintaining its aspect ratio in the final state, the component's content is scaled to fit within the component's content box. It is always aligned with the center of the component.| ![renderfit_resize_contain](figures/renderfit_resize_contain.png) |
42| RESIZE_CONTAIN_TOP_LEFT     | While maintaining its aspect ratio in the final state, the component's content is scaled to fit within the component's content box. When there is remaining space in the width direction of the component, the content is left-aligned with the component. When there is remaining space in the height direction of the component, the content is top-aligned with the component.| ![renderfit_resize_contain_top_left](figures/renderfit_resize_contain_top_left.png) |
43| RESIZE_CONTAIN_BOTTOM_RIGHT | While maintaining its aspect ratio in the final state, the component's content is scaled to fit within the component's content box. When there is remaining space in the width direction of the component, the content is right-aligned with the component. When there is remaining space in the height direction of the component, the content is bottom-aligned with the component.| ![renderfit_resize_contain_bottom_right](figures/renderfit_resize_contain_bottom_right.png) |
44| RESIZE_COVER                | While maintaining its aspect ratio in the final state, the component's content is scaled to cover the component's entire content box. It is always aligned with the center of the component, so that its middle part is displayed.| ![renderfit_resize_cover](figures/renderfit_resize_cover.png) |
45| RESIZE_COVER_TOP_LEFT       | While maintaining its aspect ratio in the final state, the component's content is scaled to cover the component's entire content box. When there is remaining space in the width direction, the content is left-aligned with the component, so that its left part is displayed. When there is remaining space in the height direction, the content is top-aligned with the component, so that its top part is displayed.| ![renderfit_resize_cover_top_left](figures/renderfit_resize_cover_top_left.png) |
46| RESIZE_COVER_BOTTOM_RIGHT   | While maintaining its aspect ratio in the final state, the component's content is scaled to cover the component's entire content box. When there is remaining space in the width direction, the content is right-aligned with the component, so that its right part is displayed. When there is remaining space in the height direction, the content is bottom-aligned with the component, so that its bottom part is displayed.| ![renderfit_resize_cover_bottom_right](figures/renderfit_resize_cover_bottom_right.png) |
47
48> **NOTE**
49>
50> - In the illustrative diagrams, the blue area indicates the content, and the orange area indicates the component content box.
51> - Different render fit modes create different effects during the width and height animation process. Choose the one that best fits your need.
52
53## Example
54
55```ts
56// xxx.ets
57@Entry
58@Component
59struct RenderFitExample {
60  @State width1: number = 100;
61  @State height1: number = 30;
62  flag: boolean = true;
63  build() {
64    Column() {
65      Text("Hello")
66        .width(this.width1)
67        .height(this.height1)
68        .borderWidth(1)
69        .textAlign(TextAlign.Start)
70        .renderFit(RenderFit.LEFT) // The component's content stays at the final size and always aligned with the left of the component.
71        .margin(20)
72
73      Text("Hello")
74        .width(this.width1)
75        .height(this.height1)
76        .textAlign(TextAlign.Center)
77        .borderWidth(1)
78        .renderFit(RenderFit.CENTER) // The component's content stays at the final size and always aligned with the center of the component.
79        .margin(20)
80
81      Button("animate")
82        .onClick(() => {
83          animateTo({ curve: Curve.Ease }, () => {
84            if (this.flag) {
85              this.width1 = 150;
86              this.height1 = 50;
87            } else {
88              this.width1 = 100;
89              this.height1 = 30;
90            }
91            this.flag = !this.flag;
92          })
93        })
94    }.width("100%").height("100%").alignItems(HorizontalAlign.Center)
95  }
96}
97```
98
99![renderfit](figures/renderfit.gif)
100