文本选择菜单,适用于RichEditor组件通过bindSelectionMenu或Text组件通过bindSelectionMenu绑定自定义文本选择菜单,支持编辑菜单和扩展下拉菜单两种类型,可通过配置实现复制、粘贴、剪切、全选等内置功能,也可通过自定义菜单项和事件回调实现扩展功能。建议绑定鼠标右键或鼠标选中方式弹出,不支持作为普通组件单独使用。适用于富文本编辑场景,可为用户提供便捷的文本操作入口,提升文本编辑效率。
该组件从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块接口仅可在Stage模型下使用。
import { SelectionMenu, EditorMenuOptions, ExpandedMenuOptions, EditorEventInfo, SelectionMenuOptions } from '@kit.ArkUI'; 选中内容信息。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
设备行为差异: 该接口在Wearable设备上使用时,应用程序运行异常,异常信息中提示接口未定义,在其他设备中可正常调用。
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| content | RichEditorSelection | 否 | 是 | 选中的内容信息,包含选中的文本或图片片段(spans)及选择范围(selection)。 |
该示例展示了文本绑定不同触发方式的自定义文本选择菜单的效果。
import {
SelectionMenu,
EditorMenuOptions,
ExpandedMenuOptions,
EditorEventInfo,
SelectionMenuOptions
} from '@kit.ArkUI';
@Entry
@Component
struct Index {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
@State message: string = 'Hello world';
@State textSize: number = 30;
@State start: number = -1;
@State end: number = -1;
@State textStyle: RichEditorTextStyle = {};
private editorMenuOptions: Array<EditorMenuOptions> =
[
{
// $r('app.media.ic_notepad_textbold')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.ic_notepad_textbold'), action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否加粗,切换加粗/取消加粗样式
if (this.textStyle.fontWeight != FontWeight.Bold) {
this.textStyle.fontWeight = FontWeight.Bolder;
} else {
this.textStyle.fontWeight = FontWeight.Normal;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('app.media.ic_notepad_texttilt')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.ic_notepad_texttilt'), action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否斜体,切换斜体/取消斜体样式
if (this.textStyle.fontStyle == FontStyle.Italic) {
this.textStyle.fontStyle = FontStyle.Normal;
} else {
this.textStyle.fontStyle = FontStyle.Italic;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('app.media.ic_notepad_underline')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.ic_notepad_underline'),
action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
if (this.textStyle.decoration) {
// 判断当前是否有下划线,切换下划线/取消下划线样式
if (this.textStyle.decoration.type == TextDecorationType.Underline) {
this.textStyle.decoration.type = TextDecorationType.None;
} else {
this.textStyle.decoration.type = TextDecorationType.Underline;
}
} else {
this.textStyle.decoration = { type: TextDecorationType.Underline, color: Color.Black }
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('app.media.ic_notepad_fontsize')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.ic_notepad_fontsize'), action: () => {
}, builder: (): void => this.sliderPanel()
},
{
// $r('app.media.ic_notepad_textcolor')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.ic_notepad_textcolor'), action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否为橙色(枚举值或十六进制字符串),切换橙色/黑色样式
if (this.textStyle.fontColor == Color.Orange || this.textStyle.fontColor == '#FFFFA500') {
this.textStyle.fontColor = Color.Black;
} else {
this.textStyle.fontColor = Color.Orange;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
}]
private expandedMenuOptions: Array<ExpandedMenuOptions> =
[{
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '词典', action: () => {
}
}, {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '翻译', action: () => {
}
}, {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '搜索', action: () => {
}
}]
private expandedMenuOptions1: Array<ExpandedMenuOptions> = [];
private selectionMenuOptions: SelectionMenuOptions = {
editorMenuOptions: this.editorMenuOptions,
expandedMenuOptions: this.expandedMenuOptions,
controller: this.controller,
onCut: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCut' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onPaste: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onPaste' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onCopy: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCopy' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onSelectAll: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onSelectAll' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
}
};
@Builder
sliderPanel() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
Text('A').fontSize(15)
Slider({ value: this.textSize, step: 10, style: SliderStyle.InSet })
.width(210)
.onChange((value: number, mode: SliderChangeMode) => {
if (this.controller) {
let selection = this.controller.getSelection();
// 滑块拖动结束时,获取当前选中文本的最大字体大小作为起始值
if (mode == SliderChangeMode.End) {
if (this.textSize == undefined) {
this.textSize = 0;
}
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
this.textSize = Math.max(this.textSize, (item as RichEditorTextSpanResult).textStyle.fontSize);
}
})
}
// 滑块拖动中或点击时,实时更新选中文本的字体大小
if (mode == SliderChangeMode.Moving || mode == SliderChangeMode.Click) {
this.start = selection.selection[0];
this.end = selection.selection[1];
this.textSize = value;
this.controller.updateSpanStyle({
start: this.start,
end: this.end,
textStyle: { fontSize: this.textSize }
})
}
}
})
Text('A').fontSize(20).fontWeight(FontWeight.Medium)
}.borderRadius($r('sys.float.ohos_id_corner_radius_card'))
}
.shadow(ShadowStyle.OUTER_DEFAULT_MD)
.backgroundColor(Color.White)
.borderRadius($r('sys.float.ohos_id_corner_radius_card'))
.padding(15)
.height(48)
}
@Builder
MyMenu() {
Column() {
SelectionMenu(this.selectionMenuOptions)
}
.width(256)
.backgroundColor(Color.Transparent)
}
@Builder
MyMenu2() {
Column() {
SelectionMenu({
editorMenuOptions: this.editorMenuOptions,
expandedMenuOptions: this.expandedMenuOptions1,
controller: this.controller,
})
}
.width(256)
.backgroundColor(Color.Transparent)
}
@Builder
MyMenu3() {
Column() {
SelectionMenu({
editorMenuOptions: this.editorMenuOptions,
expandedMenuOptions: this.expandedMenuOptions,
controller: this.controller,
})
}
.width(256)
.backgroundColor(Color.Transparent)
}
build() {
Column() {
Button('SetSelection')
.onClick((event: ClickEvent) => {
if (this.controller) {
this.controller.setSelection(0, 2);
}
})
RichEditor(this.options)
.onReady(() => {
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Orange, fontSize: 30 } });
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Black, fontSize: 25 } });
})
.onSelect((value: RichEditorSelection) => {
if (value.selection[0] == -1 && value.selection[1] == -1) {
return;
}
this.start = value.selection[0];
this.end = value.selection[1];
})
// 绑定鼠标右键操作自定义菜单
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu3(), RichEditorResponseType.RIGHT_CLICK)
// 绑定鼠标选中操作自定义菜单
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu2(), RichEditorResponseType.SELECT)
.borderWidth(1)
.borderColor(Color.Red)
.width(200)
.height(200)
.margin(10)
}
}
}
系统暂未预置加粗、斜体等图标,示例代码使用本地资源图标,开发者使用时需自行替换editorMenuOptions中icon项的资源。
示例图为鼠标操作触发的自定义菜单弹出效果。

从API version 18开始,该示例通过设置EditorMenuOptions的属性symbolStyle,展示了使用系统Symbol图标的方法。
import {
SelectionMenu,
EditorMenuOptions,
ExpandedMenuOptions,
EditorEventInfo,
SelectionMenuOptions,
SymbolGlyphModifier
} from '@kit.ArkUI'
@Entry
@Component
struct Index {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
@State message: string = 'Hello world';
@State textSize: number = 30;
@State start: number = -1;
@State end: number = -1;
@State textStyle: RichEditorTextStyle = {};
private editorMenuOptions: Array<EditorMenuOptions> =
[
{
// $r('sys.media.wifi_router_fill')需要替换为开发者所需的图像资源文件。
icon: $r('sys.media.wifi_router_fill'),
symbolStyle: new SymbolGlyphModifier($r('sys.symbol.save')),
action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否加粗,切换加粗/取消加粗样式
if (this.textStyle.fontWeight != FontWeight.Bold) {
this.textStyle.fontWeight = FontWeight.Bolder;
} else {
this.textStyle.fontWeight = FontWeight.Normal;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('sys.media.save_button_picture')需要替换为开发者所需的图像资源文件。
icon: $r('sys.media.save_button_picture'),
symbolStyle: new SymbolGlyphModifier($r('sys.symbol.camera')),
action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否斜体,切换斜体/取消斜体样式
if (this.textStyle.fontStyle == FontStyle.Italic) {
this.textStyle.fontStyle = FontStyle.Normal;
} else {
this.textStyle.fontStyle = FontStyle.Italic;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('sys.media.waveform_folder_fill')需要替换为开发者所需的图像资源文件。
icon: $r('sys.media.waveform_folder_fill'),
symbolStyle: new SymbolGlyphModifier($r('sys.symbol.car')),
action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
if (this.textStyle.decoration) {
// 判断当前是否有下划线,切换下划线/取消下划线样式
if (this.textStyle.decoration.type == TextDecorationType.Underline) {
this.textStyle.decoration.type = TextDecorationType.None;
} else {
this.textStyle.decoration.type = TextDecorationType.Underline;
}
} else {
this.textStyle.decoration = { type: TextDecorationType.Underline, color: Color.Black }
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
},
{
// $r('app.media.app_icon')需要替换为开发者所需的图像资源文件。
icon: $r('app.media.app_icon'), action: () => {
}, builder: (): void => this.sliderPanel()
},
{
// $r('sys.media.thermometer_fill')需要替换为开发者所需的图像资源文件。
icon: $r('sys.media.thermometer_fill'), action: () => {
if (this.controller) {
let selection = this.controller.getSelection();
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
this.textStyle = span.textStyle;
let start = span.offsetInSpan[0];
let end = span.offsetInSpan[1];
let offset = span.spanPosition.spanRange[0];
// 判断当前是否为橙色(枚举值或十六进制字符串),切换橙色/黑色样式
if (this.textStyle.fontColor == Color.Orange || this.textStyle.fontColor == '#FFFFA500') {
this.textStyle.fontColor = Color.Black;
} else {
this.textStyle.fontColor = Color.Orange;
}
this.controller.updateSpanStyle({
start: offset + start,
end: offset + end,
textStyle: this.textStyle
})
}
})
}
}
}]
private expandedMenuOptions: Array<ExpandedMenuOptions> =
[{
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '词典', action: () => {
}
}, {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '翻译', action: () => {
}
}, {
// $r('app.media.startIcon')需要替换为开发者所需的图像资源文件。
startIcon: $r('app.media.startIcon'), content: '搜索', action: () => {
}
}]
private expandedMenuOptions1: Array<ExpandedMenuOptions> = []
private editorMenuOptions1: Array<EditorMenuOptions> = []
private selectionMenuOptions: SelectionMenuOptions = {
editorMenuOptions: this.editorMenuOptions,
expandedMenuOptions: this.expandedMenuOptions,
controller: this.controller,
onCut: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCut' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onPaste: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onPaste' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onCopy: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCopy' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onSelectAll: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onSelectAll' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
}
}
@Builder
sliderPanel() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) {
Text('A').fontSize(15)
Slider({ value: this.textSize, step: 10, style: SliderStyle.InSet })
.width(210)
.onChange((value: number, mode: SliderChangeMode) => {
if (this.controller) {
let selection = this.controller.getSelection();
// 滑块拖动结束时,获取当前选中文本的最大字体大小作为起始值
if (mode == SliderChangeMode.End) {
if (this.textSize == undefined) {
this.textSize = 0;
}
let spans = selection.spans;
spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
this.textSize = Math.max(this.textSize, (item as RichEditorTextSpanResult).textStyle.fontSize);
}
})
}
// 滑块拖动中或点击时,实时更新选中文本的字体大小
if (mode == SliderChangeMode.Moving || mode == SliderChangeMode.Click) {
this.start = selection.selection[0];
this.end = selection.selection[1];
this.textSize = value;
this.controller.updateSpanStyle({
start: this.start,
end: this.end,
textStyle: { fontSize: this.textSize }
})
}
}
})
Text('A').fontSize(20).fontWeight(FontWeight.Medium)
}.borderRadius($r('sys.float.ohos_id_corner_radius_card'))
}
.shadow(ShadowStyle.OUTER_DEFAULT_MD)
.backgroundColor(Color.White)
.borderRadius($r('sys.float.ohos_id_corner_radius_card'))
.padding(15)
.height(48)
}
@Builder
MyMenu() {
Column() {
SelectionMenu(this.selectionMenuOptions)
}
.width(256)
.backgroundColor(Color.Transparent)
}
@Builder
MyMenu2() {
Column() {
SelectionMenu({
editorMenuOptions: this.editorMenuOptions,
expandedMenuOptions: this.expandedMenuOptions1,
controller: this.controller,
})
}
.width(256)
.backgroundColor(Color.Transparent)
}
@Builder
MyMenu3() {
Column() {
SelectionMenu({
editorMenuOptions: this.editorMenuOptions1,
expandedMenuOptions: this.expandedMenuOptions,
controller: this.controller,
})
}
.width(256)
.backgroundColor(Color.Transparent)
}
build() {
Column() {
Button('SetSelection')
.onClick((event: ClickEvent) => {
if (this.controller) {
this.controller.setSelection(0, 2);
}
})
RichEditor(this.options)
.onReady(() => {
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Orange, fontSize: 30 } });
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Black, fontSize: 25 } });
})
.onSelect((value: RichEditorSelection) => {
if (value.selection[0] == -1 && value.selection[1] == -1) {
return;
}
this.start = value.selection[0];
this.end = value.selection[1];
})
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu3(), RichEditorResponseType.RIGHT_CLICK)
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu2(), RichEditorResponseType.SELECT)
.borderWidth(1)
.borderColor(Color.Red)
.width(200)
.height(200)
}
}
} 
该示例通过设置SelectionMenuOptions的属性backgroundSystemMaterial,展示了超薄样式的背景板材质。
该示例配图为高算力设备强档效果,组件沉浸光感效果会根据设备算力与用户在系统中设置的沉浸光感效果自适应调整,开发者无需额外适配。
从API版本26.0.0开始,SelectionMenuOptions新增backgroundSystemMaterial属性。
import {
SelectionMenu, EditorEventInfo, SelectionMenuOptions, uiMaterial
} from '@kit.ArkUI';
@Entry
@Component
struct Index {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
@State message: string = 'Hello world';
@State textStyle: RichEditorTextStyle = {};
private selectionMenuOptions: SelectionMenuOptions = {
controller: this.controller,
onCut: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCut' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onPaste: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onPaste' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onCopy: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onCopy' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
onSelectAll: (event?: EditorEventInfo) => {
if (event && event.content) {
event.content.spans.forEach((item: RichEditorTextSpanResult | RichEditorImageSpanResult, index) => {
if (typeof (item as RichEditorTextSpanResult)['textStyle'] != 'undefined') {
let span = item as RichEditorTextSpanResult;
console.info('test onSelectAll' + span.value);
console.info('test start ' + span.offsetInSpan[0] + ' end: ' + span.offsetInSpan[1]);
}
})
}
},
// 使用系统材质,以超薄样式为例
backgroundSystemMaterial: new uiMaterial.ImmersiveMaterial({
style: uiMaterial.ImmersiveStyle.ULTRA_THIN
})
};
@Builder
MyMenu() {
Column() {
SelectionMenu(this.selectionMenuOptions)
}
.width(256)
.backgroundColor(Color.Transparent)
}
build() {
Column() {
Button('SetSelection')
.onClick((event: ClickEvent) => {
if (this.controller) {
this.controller.setSelection(0, 2);
}
})
RichEditor(this.options)
.onReady(() => {
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Orange, fontSize: 30 } });
this.controller.addTextSpan(this.message, { style: { fontColor: Color.Black, fontSize: 25 } });
})
.onSelect((value: RichEditorSelection) => {
if (value.selection[0] == -1 && value.selection[1] == -1) {
return;
}
})
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu(), RichEditorResponseType.RIGHT_CLICK)
.bindSelectionMenu(RichEditorSpanType.TEXT, this.MyMenu(), RichEditorResponseType.LONG_PRESS)
.borderWidth(1)
.borderColor(Color.Red)
.width(200)
.height(200)
.margin(10)
}
}
} 未设置系统材质时:

设置系统材质时:
