The SelectionContainer component provides cross-node text selection, copying, and menu extension capabilities for multiple text nodes. It supports unified configuration of the caret color and background color of selected text, flexible text concatenation policies, and custom selection menus and menu extension options. It is suitable for scenarios where continuous text selection, unified copying, style customization, and menu extension are required across multiple Text components. It resolves the problem of fragmented text selection experience in multi-Text component scenarios and improves the user interaction experience in complex text layouts.
Since: 26.0.0
SelectionContainer(value?: SelectionContainerOptions)
Creates a SelectionContainer component.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | SelectionContainerOptions | No | Initial configuration options of the component. |
Universal attributes are supported.
copyOption(value: Optional<CopyOptions>)
Sets the copy option for the component. If this attribute is not used, the default value is CopyOptions.InApp.
If the Text child component has explicitly set copyOption, the configuration of the Text child component takes precedence. If this attribute is not set, the configuration of SelectionContainer is used.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | Optional<CopyOptions> | Yes | Copy options, used to set the copyable range of text. For details, see the CopyOptions enumeration. |
caretColor(color: Optional<ResourceColor>)
Sets the caret color of the selected text. If this attribute is not used, the default caret color is '#007DFF' (blue).
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| color | Optional<ResourceColor> | Yes | Caret color. |
selectedBackgroundColor(color: Optional<ResourceColor>)
Sets the background color of the selected text. If this attribute is not used, the default background color of the selected text is '#007DFF' (blue). If the opacity is not set, the default opacity is 20%.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| color | Optional<ResourceColor> | Yes | Background color of the selected text. |
enableHapticFeedback(isEnabled: Optional<boolean>)
Sets whether to enable haptic feedback. If this attribute is not used, haptic feedback is enabled by default.
When haptic feedback is enabled, you need to set the requestPermissions field in the module.json5 configuration file of the project to enable the vibration permission. The configuration is as follows:
"requestPermissions": [
{
"name": "ohos.permission.VIBRATE"
}
] Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| isEnabled | Optional<boolean> | Yes | Whether to enable haptic feedback. true indicates to enable, and false indicates to disable. |
textJoinStyle(style: Optional<SelectionContainerTextJoinStyle>)
Sets the concatenation method for the aggregated text in SelectionContainer. If this attribute is not used, the default value is SelectionContainerTextJoinStyle.NEWLINE, which means that different text nodes are concatenated with newline characters (\n).
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| style | Optional<SelectionContainerTextJoinStyle> | Yes | Concatenation method of the aggregated text. |
onTextSelectionChange(callback: Optional<Callback<Array<string>>>)
Triggered when the selected text in SelectionContainer changes. This API returns the result asynchronously through a callback.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
onWillCopy(callback: Optional<Callback<string, boolean>>)
Triggered before a copy operation is performed. This API returns the result asynchronously through a callback.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
onCopy(callback: Optional<Callback<string>>)
Triggered when the copy button on the selection menu is tapped after the selection menu is displayed by long-pressing the inner area of the text. Only text copying is supported. This API returns the result asynchronously through a callback.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
Provides the concatenation method for text aggregation.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Value | Description |
|---|---|---|
| NEWLINE | 0 | Different text nodes are concatenated with newline characters (\n). |
| DIRECT | 1 | Different text nodes are concatenated directly without separators. |
Provides the controller of the SelectionContainer component.
Since: 26.0.0
clearTextSelection(): void
Clears the current text selection state of SelectionContainer. If the selection menu is being displayed, it is also closed.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
Provides the initial configuration options of the component.
Since: 26.0.0
Atomic service API: This API can be used in atomic services since API version 26.0.0.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| controller | SelectionContainerController | No | No | Controller of SelectionContainer. |
This example demonstrates how to select text across multiple Text components, concatenate the selected text, and handle copy callbacks by using SelectionContainer, copyOption, textJoinStyle, onTextSelectionChange, onWillCopy, and onCopy.
Since API version 26.0.0, the SelectionContainer component and APIs such as copyOption are added.
import {
SelectionContainer,
SelectionContainerAttribute,
SelectionContainerTextJoinStyle
} from '@kit.ArkUI';
@Entry
@Component
struct SelectionContainerExample1 {
@State selectedParts: string[] = [];
@State copiedText: string = '';
build() {
Column({ space: 12 }) {
Text('Long press the area below and select text across nodes.')
.fontSize(16)
SelectionContainer() {
Column({ space: 8 }) {
Text('First paragraph: SelectionContainer supports selecting text across multiple Text components.')
.fontSize(18)
.copyOption(CopyOptions.InApp)
Text('Second paragraph: The selected result is concatenated in the visual order of the Text components.')
.fontSize(18)
.copyOption(CopyOptions.InApp)
Text('Third paragraph: You can listen for selection changes, pre-copy validation, and copy completion events.')
.fontSize(18)
.copyOption(CopyOptions.InApp)
}
}
.copyOption(CopyOptions.InApp)
.textJoinStyle(SelectionContainerTextJoinStyle.NEWLINE)
.caretColor(Color.Red)
.selectedBackgroundColor('#33007DFF')
.onTextSelectionChange((value: Array<string>) => {
this.selectedParts = value;
console.info(`Selected text changed: ${JSON.stringify(value)}`);
})
.onWillCopy((value: string) => {
this.copiedText = `Ready to copy: ${value}`;
console.info(`Text ready to copy: ${value}`);
return true;
})
.onCopy((value: string) => {
this.copiedText = `Copy succeeded: ${value}`;
console.info(`Copied text: ${value}`);
})
.border({ width: 1, color: '#DCDCDC' })
.padding(12)
.width('100%')
Text(`Selected content: ${this.selectedParts.join(' | ')}`)
.fontSize(14)
.fontColor('#666666')
Text(this.copiedText)
.fontSize(14)
.fontColor('#666666')
}
.width('100%')
.padding(16)
}
} 