harmony 鸿蒙Text Component Common APIs

  • 2025-06-12
  • 浏览 (5)

Text Component Common APIs

NOTE

This topic covers the common APIs of text components. 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.

CaretStyle10+

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
width Length No Caret size. It cannot be set in percentage.
Default value: ‘2vp’.
color ResourceColor No Caret color.
Default value: ’#ff007dff’.

LayoutManager12+

Implements a LayoutManager object.

NOTE

After the text content is changed, you must wait for the layout to be completed before you can obtain the most up-to-date layout information.

Objects to Import

The following uses the Text component as an example.

controller: TextController = new TextController()
let layoutManager: LayoutManager = this.controller.getLayoutManager();

getLineCount

getLineCount(): number

Obtains the total number of lines in the component.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
number Total number of lines in the component.

getGlyphPositionAtCoordinate

getGlyphPositionAtCoordinate(x: number, y: number): PositionWithAffinity

Obtains the position of a glyph close to a given coordinate.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
x number Yes X coordinate relative to the component.
Unit: px
y number Yes Y coordinate relative to the component.
Unit: px

Return value

Type Description
PositionWithAffinity Position of the glyph.

getLineMetrics

getLineMetrics(lineNumber: number): LineMetrics

Obtains the information about the specified line, including line metrics, text style information, and font properties.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
lineNumber number Yes Line number, which is zero-based.

Return value

Type Description
LineMetrics Information about the specified line, including line metrics, text style information, and font properties.
Returns an invalid value if the line number is less than 0 or exceeds the actual number of lines.

getRectsForRange14+

getRectsForRange(range: TextRange, widthStyle: RectWidthStyle, heightStyle: RectHeightStyle): Array<TextBox>

Obtains the drawing area information for characters or placeholders within a specified text range, based on the specified width and height of a rectangle.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
range TextRange Yes Text range for which to obtain the area information.
widthStyle RectWidthStyle Yes Width of the rectangle.
heightStyle RectHeightStyle Yes Height of the rectangle.

Return value

Type Description
Array<TextBox> Array holding the rectangles obtained.

PositionWithAffinity12+

Describes the position and affinity of a glyph.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read Only Optional Description
position number Yes No Index of the glyph relative to the paragraph. The value is an integer.
affinity Affinity Yes Yes Affinity of the position.

TextMenuItemId12+

Defines the unique identifier for a custom menu item. It is used to identify menu items. The IDs for built-in menu items are listed in the table below.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Properties

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Read Only Optional Description
CUT TextMenuItemId Yes Yes ID for the cut menu item.
Atomic service API: This API can be used in atomic services since API version 12.
COPY TextMenuItemId Yes Yes ID for the paste menu item.
Atomic service API: This API can be used in atomic services since API version 12.
PASTE TextMenuItemId Yes Yes ID for the paste menu item.
Atomic service API: This API can be used in atomic services since API version 12.
SELECT_ALL TextMenuItemId Yes Yes ID for the select-all menu item.
Atomic service API: This API can be used in atomic services since API version 12.
COLLABORATION_SERVICE TextMenuItemId Yes Yes ID for the collaboration service menu item.
Atomic service API: This API can be used in atomic services since API version 12.
CAMERA_INPUT TextMenuItemId Yes Yes ID for the camera input menu item.
Atomic service API: This API can be used in atomic services since API version 12.
AI_WRITER13+ TextMenuItemId Yes Yes ID for the menu item involving text enhancement features, such as polishing, summary extraction, and formatting, for selected text. This menu item requires foundation model capabilities to function.
Atomic service API: This API can be used in atomic services since API version 13.
TRANSLATE15+ TextMenuItemId Yes Yes ID for the translate menu item.
Atomic service API: This API can be used in atomic services since API version 15.
SHARE18+ TextMenuItemId Yes Yes ID for the share menu item, which launches a window for sharing the selected text.
Atomic service API: This API can be used in atomic services since API version 18.
SEARCH18+ TextMenuItemId Yes Yes ID for the search menu item, which launches a browser to search the selected text.
Atomic service API: This API can be used in atomic services since API version 18.

of

static of(id: ResourceStr): TextMenuItemId

Creates a TextMenuItemId object based on id.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
id ResourceStr Yes Menu ID.

Return value

Type Description
TextMenuItemId TextMenuItemId object.

equals

equals(id: TextMenuItemId): boolean

Checks whether this TextMenuItemId object is the same as another TextMenuItemId object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
id TextMenuItemId Yes ID of the TextMenuItemId object to compare.

Return value

Type Description
boolean Whether the two TextMenuItemId objects are the same.

TextMenuItem12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
content ResourceStr Yes Menu name.
icon ResourceStr No Menu icon.
Online images are not supported.
id TextMenuItemId Yes Menu ID.
labelInfo15+ ResourceStr No Shortcut key hint.
This field is only supported on 2-in-1 devices.
Atomic service API: This API can be used in atomic services since API version 15.

EditMenuOptions

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

onCreateMenu12+

onCreateMenu(menuItems: Array<TextMenuItem>): Array<TextMenuItem>

Creates a menu with the specified menu items.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
menuItems Array<TextMenuItem> Yes Menu items.
NOTE
Changes to the name of the default menu item do not take effect.

Return value

Type Description
Array<TextMenuItem> Menu items set.
NOTE
The icon of the default menu item is not returned.

onMenuItemClick12+

onMenuItemClick(menuItem: TextMenuItem, range: TextRange): boolean

Called when the specified menu item is clicked.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
menuItem TextMenuItem Yes Menu item.
range TextRange Yes Selected text.

Return value

Type Description
boolean Execution logic of the menu item.
Returns true if the default system logic is intercepted and only the custom logic is executed.
Returns false if the custom logic is executed before the default system logic.

TextRange12+

Defines the text range.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

|Name|Type|Mandatory|Description| |–|–|–|–| |start|number|No|Start index.| |end|number|No|End index.|

EditableTextOnChangeCallback12+

type EditableTextOnChangeCallback = (value: string, previewText?: PreviewText, options?: TextChangeOptions) => void

Represents the callback triggered when the content in the text box changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

|Name|Type|Mandatory|Description| |–|–|–|–| |value|string|Yes|Text displayed in the text box.| |previewText|PreviewText|No|Information about the preview text, including its start position and text content.| |options15+|TextChangeOptions|No|Information about the text change, including the selection range before and after the change, the text content before the change, and the preview text information.|

TextDataDetectorType11+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
PHONE_NUMBER 0 Phone number.
URL 1 URL.
EMAIL 2 Email address.
ADDRESS 3 Address.
DATE_TIME12+ 4 Time.

TextDeleteDirection12+

Defines the direction for deleting text.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
BACKWARD 0 Backward delete.
FORWARD 1 Forward delete.

MenuType13+

Enumerates the menu types.

Atomic service API: This API can be used in atomic services since API version 13.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
SELECTION_MENU 0 Text selection menu.
PREVIEW_MENU 1 Preview menu.

KeyboardAppearance15+

Enumerates the appearance modes of the keyboard.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
NONE_IMMERSIVE 0 Default appearance mode, not using immersive style.
IMMERSIVE 1 Immersive style, following the system color mode.
LIGHT_IMMERSIVE 2 Immersive style in light mode.
DARK_IMMERSIVE 3 Immersive style in dark mode.

InsertValue12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
insertOffset number Yes Position of the inserted text.
insertValue string Yes Content of the inserted text.

DeleteValue12+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
deleteOffset number Yes Position of the deleted text.
direction TextDeleteDirection Yes Direction for deleting the text.
deleteValue string Yes Content of the deleted text.

TextDataDetectorConfig11+

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
types TextDataDetectorType[] Yes Entity types for text recognition. Values null and [] indicate that all types of entities can be recognized.
onDetectResultUpdate (result: string) => void No Callback invoked when text recognition succeeds.
- result: text recognition result, in JSON format.
color12+ ResourceColor No Entity color when text recognition succeeds.
Default value: ’#ff0a59f7’
decoration12+ DecorationStyleInterface No Style of the entity decorative line when text recognition succeeds.
Default value:
{
type: TextDecorationType.Underline,
color: same as the entity
style: TextDecorationStyle.SOLID
}

PreviewText12+

Preview text.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
offset number Yes Start position of the preview text.
value string Yes Content of the preview text.

FontSettingOptions12+

Defines the font settings, including smooth and continuous adjustments for the font weight for components in your application.

Widget capability: This API can be used in ArkTS widgets since API version 12.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
enableVariableFontWeight boolean No Whether to support variable font weight adjustment.
Default value: false.
true: Support variable font weight adjustment.
false: Do not support variable font weight adjustment.

OnDidChangeCallback12+

type OnDidChangeCallback = (rangeBefore: TextRange, rangeAfter: TextRange) => void

Represents the callback invoked after text changes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

|Name|Type|Mandatory|Description| |–|–|–|–| |rangeBefore|TextRange|Yes|Range of the text to be changed.| |rangeAfter|TextRange|Yes|Range of the text added.|

StyledStringChangedListener12+

Defines the listener for changes of the styled string text content.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

|Name|Type|Mandatory|Description| |–|–|–|–| |onWillChange|Callback<StyledStringChangeValue, boolean>|No|Callback invoked when text is about to change.| |onDidChange|OnDidChangeCallback|No|Callback invoked when text is changed.|

StyledStringChangeValue12+

Describes the text changes of the styled string.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

|Name|Type|Mandatory|Description| |–|–|–|–| |range|TextRange|Yes|Range of the styled string to be replaced in the original string.| |replacementString|StyledString|Yes|Styled string used for replacement.| |previewText|StyledString|No|Preview styled string.|

TextBaseController12+

Defines a text selection controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

setSelection12+

setSelection(selectionStart: number, selectionEnd: number, options?: SelectionOptions): void

Sets the range of content selection. The selected content is highlighted.

If both selectionStart and selectionEnd are set to -1, the entire content is selected.

The component must be focused for the API call to have effect.

Since API version 12, on 2-in-1 devices, regardless of the value of options, calling the setSelection API will not display a menu; if a menu is already open, calling the API will close it.

On non-2-in-1 devices, when options is set to MenuPolicy.DEFAULT, the following rules apply after the API is called:

  1. If the component has a menu with a selection handle, the menu remains open and is relocated according to the selection.

  2. If the component has a menu without a selection handle, the menu remains open and its position remains unchanged.

  3. If there is no menu open, no menu will appear after the selection.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
selectionStart number Yes Start position of the selection.
Values less than 0 are treated as 0.
selectionEnd number Yes End position of the selection.
If the value exceeds the text length, the current text length is used instead.
options SelectionOptions No Configuration of options.

closeSelectionMenu12+

closeSelectionMenu(): void

Closes the custom or default text selection menu.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

getLayoutManager12+

getLayoutManager(): LayoutManager

Obtains a LayoutManager object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
LayoutManager Implements a LayoutManager object.

TextEditControllerEx12+

Implements an extended text editing controller.

Inherits TextBaseController.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

isEditing12+

isEditing(): boolean

Obtains the editing status of the rich text.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
boolean Editing status of the rich text. true means that the text is in editable state, and false means the opposite.

stopEditing12+

stopEditing(): void

Stops editing.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

getCaretOffset12+

getCaretOffset(): number

Obtains the current position of the cursor.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
number Position of the cursor.

setCaretOffset12+

setCaretOffset(offset: number): boolean

Sets the cursor offset.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
offset number Yes Offset of the cursor. If the offset is outside the range of all content, the setting fails.

Return value

Type Description
boolean Whether the cursor is set successfully.

getPreviewText12+

getPreviewText?(): PreviewText

Obtains the preview text.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
PreviewText Preview text.

StyledStringController12+

Defines a styled string controller.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

setStyledString12+

setStyledString(styledString: StyledString): void

Sets the styled string displayed in the rich text component.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Name Type Mandatory Description
styledString StyledString Yes Styled string to set.
NOTE
The child class MutableStyledString of StyledString can also serve as the argument.

getStyledString12+

getStyledString(): MutableStyledString

Obtains the styled string displayed in the rich text component.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

Type Description
MutableStyledString Styled string displayed in the rich text component.

DecorationStyleResult12+

Provides the text decorative line information returned by the backend.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
type TextDecorationType Yes Type of the text decorative line.
color ResourceColor Yes Color of the text decorative line.
style TextDecorationStyle No Style of the text decorative line.

LineMetrics12+

type LineMetrics = LineMetrics

Describes the measurement information of a single line of text in the text layout.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Type Description
LineMetrics Measurement information of a single line of text in the text layout.

Affinity12+

type Affinity = Affinity

Enumerates the affinity modes.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Type Description
Affinity Affinity mode enum.

TextBox14+

type TextBox = TextBox

Describes the rectangle that holds the text.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

Type Description
TextBox Rectangle that holds the text.

RectHeightStyle14+

type RectHeightStyle = RectHeightStyle

Enumerates the rectangle height styles.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

Type Description
RectHeightStyle Rectangle height style enum.

RectWidthStyle14+

type RectWidthStyle = RectWidthStyle

Enumerates the rectangle width styles.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.ArkUI.ArkUI.Full

Type Description
RectWidthStyle Rectangle width style enum.

TextChangeOptions15+

Provides information about the text before and after a change, including the selection ranges.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
rangeBefore TextRange Yes Selection range before the change.
rangeAfter TextRange Yes Selection range after the change.
oldContent string Yes Text content before the change.
oldPreviewText PreviewText Yes Preview text before the change.

EditableTextChangeValue15+

Provides detailed information of text changes, including preview text.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
content string Yes Current text content.
previewText PreviewText No Preview text.
options TextChangeOptions No Information about the text change.

TextMenuShowMode16+

Enumerates the display modes for context menus on selection.

Atomic service API: This API can be used in atomic services since API version 16.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Value Description
DEFAULT 0 The menu is displayed in the current window.
PREFER_WINDOW 1 The menu is preferentially displayed in a separate window. If a separate window is not supported, the menu is displayed in the current window.
NOTE
The context menu on selection cannot be displayed in a separate window under the following scenarios:
Windows other than the main application window, application subwindows, system modal windows, and system home screen windows
DevEco Studio Previewer
UIExtension
Text component that is already displayed in a child window (for example, Popup, Dialog, Toast, and Menu)
TextInput or TextArea components that support AutoFill

TextMenuOptions16+

Provides the options for customizing the context menu on selection.

Atomic service API: This API can be used in atomic services since API version 16.

System capability: SystemCapability.ArkUI.ArkUI.Full

Name Type Mandatory Description
showMode TextMenuShowMode No Menu display mode.
Default value: TextMenuShowMode.DEFAULT

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙advanced.Counter

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

0  赞