Class (ComponentUtils)
Provides API for obtaining the coordinates and size of the drawing area of a component.
NOTE
The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The initial APIs of this class are supported since API version 10.
In the following API examples, you must first use getComponentUtils() in UIContext to obtain a ComponentUtils instance, and then call the APIs using the obtained instance.
getRectangleById
getRectangleById(id: string): componentUtils.ComponentInfo
Obtains the size, position, translation, scaling, rotation, and affine matrix information of the specified component.
NOTE
This API should be called after the target component's layout is complete to obtain its size information. It is recommended that you use this API within onAppear.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| id | string | Yes | Unique component ID. |
Return value
| Type | Description |
|---|---|
| componentUtils.ComponentInfo | Size, position, translation, scaling, rotation, and affine matrix information of the component. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 100001 | UI execution context not found. |
Example
import { ComponentUtils } from '@kit.ArkUI';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
let componentUtils: ComponentUtils = this.getUIContext().getComponentUtils();
let modePosition = componentUtils.getRectangleById("HelloWorld");
let width = modePosition.size.width; // Obtain the component width.
let height = modePosition.size.height; // Obtain the component height.
let localOffsetX = modePosition.localOffset.x; // Obtain the x-axis offset of the component relative to its parent component.
let localOffsetY = modePosition.localOffset.y; // Obtain the y-axis offset of the component relative to its parent component.
console.info(`width: ${width}, height: ${height}, localOffsetX: ${localOffsetX}, localOffsetY: ${localOffsetY}`);
})
}
.height('100%')
.width('100%')
}
}