鴻蒙ArkUI實戰之TextArea組件、RichEditor組件、RichText組件、Search組件的使用

本文接上篇繼續更新ArkUI中組件的使用,本文介紹的組件有TextArea組件、RichEditor組件、RichText組件、Search組件,這幾個組件的使用對應特定場景,使用時更加需要注意根據需求去使用

TextArea組件

官方文檔:

TextArea-文本與輸入-ArkTS組件-ArkUI(方舟UI框架)-應用框架 - 華為HarmonyOS開發者

?參數表:

名稱類型必填說明
placeholderResourceStr

設置無輸入時的提示文本。輸入內容后,提示文本不顯示。

僅設置placeholder屬性時,手柄依然跟隨拖動,手柄松開后光標停留在文字開頭位置。

textResourceStr

設置輸入框當前的文本內容。

建議通過onChange事件將狀態變量與文本實時綁定,

避免組件刷新時TextArea中的文本內容異常。

從API version 10開始,該參數支持$$雙向綁定變量。

controller8+TextAreaController設置TextArea控制器。

?TextArea組件多行文本輸入框組件,當輸入的文本內容超過組件寬度時會自動換行顯示,在以下案例中,展示了三個多行文本輸入框,第一個是默認的,并沒有給高度,第二個給了高度,以及占位字符,第三個加上了,最大字符數,以及顯示計數器,在輸入時可通過onChange回調函數獲取到輸入框中的值

屬性名稱描述可選值或默認值
placeholder設置文本框的提示文本字符串值,如代碼中的 '請輸入內容'
width設置文本框的寬度可設置為數字、百分比等,如代碼中的 '100%'
height設置文本框的高度可設置為像素值等,如代碼中的 150
maxLength設置文本框的最大輸入長度正整數,如代碼中的 20
showCounter是否顯示輸入計數器boolean 值,如代碼中的 true

?代碼部分:

@Component
export struct TextAreaCase {build() {NavDestination() {Column({ space: 20 }) {TextArea()TextArea({placeholder: '請輸入內容',}).width('100%').height(150)TextArea({placeholder: '請輸入內容',}).width('100%').height(150).maxLength(20)// 設置最大長度.showCounter(true) // 顯示計數器}.justifyContent(FlexAlign.Center).height('100%').width('80%')}.height('100%').width('100%').title('展示TextArea')}
}@Builder
function TextAreaCaseBuilder() {TextAreaCase()
}

?展示效果:

?RichEditor組件

官方文檔:

RichEditor-文本與輸入-ArkTS組件-ArkUI(方舟UI框架)-應用框架 - 華為HarmonyOS開發者

?支持圖文混排和文本交互式編輯的組件,這個組件屬于是用法·多樣化,一般使用場景在,對輸入內容有多中交互,以及輸入內容有多樣化需求的場景中

屬性/設置描述可選值或默認值
options配置?RichEditor?的選項,如控制器?controllerRichEditorOptions?對象,包含控制器等配置項
onReady組件準備就緒時的回調函數,用于注冊事件或初始化操作回調函數,如代碼中的注冊文本變化回調和設置屬性字符串
controllerRichEditor?的控制器,用于操作和獲取組件的內容和狀態RichEditorController?或?RichEditorStyledStringController?對象
StyledString屬性字符串,包含文本和樣式信息包含文本和樣式范圍的對象
MutableStyledString可變屬性字符串,允許動態修改文本和樣式包含初始文本和樣式范圍的對象

?這里借用官方案例做展示,這里需要關注的點在于,RichEditor({controller:控制器}),這里邊的控制器,這個編輯器里的組件可以通過控制器控制其行為

?代碼部分:

import { image } from '@kit.ImageKit';
import { LengthMetrics } from '@kit.ArkUI';@Component
export struct RichEditorCase {stringLength: number = 0;imagePixelMap: image.PixelMap | undefined = undefined;@State selection: string = "";@State content: string = "";@State range: string = "";@State replaceString: string = "";@State rangeBefore: string = "";@State rangeAfter: string = "";richEditorStyledString: MutableStyledString = new MutableStyledString("");textStyle: TextStyle = new TextStyle({fontWeight: FontWeight.Lighter,fontFamily: 'HarmonyOS Sans',fontColor: Color.Green,fontSize: LengthMetrics.vp(30),fontStyle: FontStyle.Normal})fontStyle1: TextStyle = new TextStyle({ fontColor: Color.Blue });fontStyle2: TextStyle = new TextStyle({fontWeight: FontWeight.Bolder,fontFamily: 'Arial',fontColor: Color.Orange,fontSize: LengthMetrics.vp(30),fontStyle: FontStyle.Italic})controller1: RichEditorController = new RichEditorController()options1: RichEditorOptions = { controller: this.controller1 };// 創建屬性字符串對象mutableStyledString: MutableStyledString = new MutableStyledString("初始屬性字符串",[{start: 0,length: 5,styledKey: StyledStringKey.FONT,styledValue: this.fontStyle1}]);styledString: StyledString = new StyledString("插入屬性字符串",[{start: 2,length: 4,styledKey: StyledStringKey.FONT,styledValue: this.fontStyle2}]);controller: RichEditorStyledStringController = new RichEditorStyledStringController();options: RichEditorStyledStringOptions = { controller: this.controller };// 文本內容變化回調contentChangedListener: StyledStringChangedListener = {onWillChange: (value: StyledStringChangeValue) => {this.range = '[ ' + value.range.start + ' , ' + value.range.end + ' ]';this.replaceString = value.replacementString.getString();return true;},onDidChange: (rangeBefore, rangeAfter) => {this.rangeBefore = '[ ' + rangeBefore.start + ' , ' + rangeBefore.end + ' ]';this.rangeAfter = '[ ' + rangeAfter.start + ' , ' + rangeAfter.end + ' ]';}}async aboutToAppear() {console.info("aboutToAppear initial imagePixelMap");this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.app_icon'));}build() {NavDestination() {Column({ space: 6 }) {Column() {Text("選中區信息").fontSize(20).width("100%")Text("selection range: " + this.selection).width("100%")Text("selection content: " + this.content).width("100%")}.width("100%").height("10%")Column() {Text("onWillChange回調信息").fontSize(20).width("100%")Text("range: " + this.range).width("100%")Text("replacementString: " + this.replaceString).width("100%")Text("onWillChange回調信息").fontSize(20).width("100%")Text("rangeBefore: " + this.rangeBefore).width("100%")Text("rangeAfter: " + this.rangeAfter).width("100%")}.borderWidth(1).borderColor(Color.Black).width("100%").height("20%")RichEditor(this.options).onReady(() => {// 注冊文本變化回調this.controller.onContentChanged(this.contentChangedListener);// 設定組件展示的屬性字符串this.controller.setStyledString(this.mutableStyledString);}).height("20%").width("100%")RichEditor(this.options1).onReady(() => {this.controller1.addTextSpan("把這些文字轉換成屬性字符串");}).height("10%").width("100%").borderWidth(1).borderColor(Color.Black)Row({ space: 2 }) {Button("插入圖片").stateEffect(true).onClick(() => {if (this.imagePixelMap !== undefined) {let imageStyledString = new MutableStyledString(new ImageAttachment({value: this.imagePixelMap,size: { width: 50, height: 50 },layoutStyle: { borderRadius: LengthMetrics.vp(10) },verticalAlign: ImageSpanAlignment.BASELINE,objectFit: ImageFit.Contain}))// 獲取組件展示的屬性字符串this.richEditorStyledString = this.controller.getStyledString();this.richEditorStyledString.appendStyledString(imageStyledString);// 使插入圖片后的屬性字符串展示在組件上this.controller.setStyledString(this.richEditorStyledString);this.controller.setCaretOffset(this.richEditorStyledString.length);}})Button("插入文本").onClick(() => {// 獲取組件展示的屬性字符串this.richEditorStyledString = this.controller.getStyledString();this.richEditorStyledString.appendStyledString(this.styledString);// 使插入文本后的屬性字符串展示在組件上this.controller.setStyledString(this.richEditorStyledString);this.controller.setCaretOffset(this.richEditorStyledString.length);})Button("刪除選中內容").onClick(() => {// 獲取選中范圍let richEditorSelection = this.controller.getSelection();let start = richEditorSelection.start ? richEditorSelection.start : 0;let end = richEditorSelection.end ? richEditorSelection.end : 0;if (start < 0 || end <= start) {return;}// 獲取組件展示的屬性字符串this.richEditorStyledString = this.controller.getStyledString();this.richEditorStyledString.removeString(start, end - start);// 使刪除內容后的屬性字符串展示在組件上this.controller.setStyledString(this.richEditorStyledString);})}Row({ space: 2 }) {Button("獲取選中內容").onClick(() => {// 獲取選中范圍let richEditorSelection = this.controller.getSelection();let start = richEditorSelection.start ? richEditorSelection.start : 0;let end = richEditorSelection.end ? richEditorSelection.end : 0;// 獲取組件展示的屬性字符串this.richEditorStyledString = this.controller.getStyledString();this.selection = '[ ' + start + ' , ' + end + ' ]';if (start == end) {this.content = "";} else {this.content = this.richEditorStyledString.subStyledString(start, end - start).getString();}})Button("更新選中樣式").onClick(() => {// 獲取選中范圍let richEditorSelection = this.controller.getSelection();let start = richEditorSelection.start ? richEditorSelection.start : 0;let end = richEditorSelection.end ? richEditorSelection.end : 0;if (start < 0 || end <= start) {return;}// 獲取組件展示的屬性字符串this.richEditorStyledString = this.controller.getStyledString();this.richEditorStyledString.setStyle({start: start,length: end - start,styledKey: StyledStringKey.FONT,styledValue: this.textStyle})// 使變更樣式后的屬性字符串展示在組件上this.controller.setStyledString(this.richEditorStyledString);})}Row({ space: 2 }) {//將屬性字符串轉換成span信息Button("調用fromStyledString").onClick(() => {this.controller1.addTextSpan("調用fromStyledString:" +JSON.stringify(this.controller1.fromStyledString(this.mutableStyledString)))})//將給定范圍的組件內容轉換成屬性字符串Button("調用toStyledString").onClick(() => {this.controller.setStyledString(this.controller1.toStyledString({ start: 0, end: 13 }))})}}.justifyContent(FlexAlign.Center).height('100%').width('100%')}.height('100%').width('100%').title('展示RichEditor')}private async getPixmapFromMedia(resource: Resource) {let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({bundleName: resource.bundleName,moduleName: resource.moduleName,id: resource.id})let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))let createPixelMap: image.PixelMap = await imageSource.createPixelMap({desiredPixelFormat: image.PixelMapFormat.RGBA_8888})await imageSource.release()return createPixelMap}
}@Builder
function RichEditorCaseBuilder() {RichEditorCase()
}

?效果展示:

RichText組件

官方文檔:

RichText-文本與輸入-ArkTS組件-ArkUI(方舟UI框架)-應用框架 - 華為HarmonyOS開發者

富文本組件,解析并顯示HTML格式文本,可以在頁面中展示HTML格式的文本,接收一個HTML格式字符串,這里的重點在于RichText("HTML格式字符串")

屬性名稱描述可選值或默認值
width設置組件的寬度可設置為像素值、百分比等,如代碼中的 '80%'
height設置組件的高度可設置為像素值等,如代碼中的 300
border設置邊框屬性包含邊框寬度、顏色、圓角等,如代碼中的?.border({ width: 4 })?等
borderWidth設置邊框寬度可設置為像素值等,如代碼中的 1
borderColor設置邊框顏色顏色值,如代碼中的 '#E6B5B2'
borderRadius設置邊框圓角半徑可設置為像素值等,如代碼中的 10

?代碼部分:

@Component
export struct RichTextCase {@State data: string = '<h1 style="text-align: center;">h1標題</h1>' +'<h1 style="text-align: center;"><i>h1斜體</i></h1>' +'<h1 style="text-align: center;"><u>h1下劃線</u></h1>' +'<h2 style="text-align: center;">h2標題</h2>' +'<h3 style="text-align: center;">h3標題</h3>' +'<p style="text-align: center;">p常規</p><hr/>' +'<div style="width: 500px;height: 500px;border: 1px solid;margin: 0 auto;">' +'<p style="font-size: 35px;text-align: center;font-weight: bold; color: rgb(24,78,228)">字體大小35px,行高45px</p>' +'<p style="background-color: #e5e5e5;line-height: 45px;font-size: 35px;text-indent: 2em;">' +'<p>這是一段文字這是一段文字這是一段文字這是一段文字這是一段文字這是一段文字這是一段文字這是一段文字這是一段文字</p>';build() {NavDestination() {Column({ space: 20 }) {RichText(this.data).width('80%').height(300).border({ width: 4 }).borderWidth(1).borderColor('#E6B5B2').borderRadius(10)}.justifyContent(FlexAlign.Center).height('100%').width('100%')}.height('100%').width('100%').title('展示RichText')}
}@Builder
function RichTextCaseBuilder() {RichTextCase()
}

?效果展示:

Search組件

官方文檔:

Search-文本與輸入-ArkTS組件-ArkUI(方舟UI框架)-應用框架 - 華為HarmonyOS開發者

搜索框組件?,是一個鴻蒙系統重標準的搜索框樣式,可修改其參數以及屬性去根據需求自定義效果,這里是參數表

參數名類型必填說明
valuestring

設置當前顯示的搜索文本內容。

從API version 10開始,該參數支持$$雙向綁定變量。

placeholderResourceStr10+設置無輸入時的提示文本。
iconstring

設置搜索圖標路徑,默認使用系統搜索圖標。

說明:

icon的數據源支持本地圖片和網絡圖片。

- 支持的圖片格式包括png、jpg、bmp、svg、gif、pixelmap和heif。

- 支持Base64字符串。格式data:image/[png|jpeg|bmp|webp|heif];base64,[base64 data], 其中[base64 data]為Base64字符串數據。

如果與屬性searchIcon同時設置,則searchIcon優先。

controllerSearchController設置Search組件控制器。

?代碼部分:

這里通過searchButton屬性設置了搜索框尾部的按鈕字樣

@Component
export struct SearchCase {build() {NavDestination() {Column({ space: 20 }) {Search({ placeholder: '請輸入內容' }).searchButton('搜索')}.justifyContent(FlexAlign.Center).height('100%').width('80%')}.height('100%').width('100%').title('展示Search')}
}@Builder
function SearchCaseBuilder() {SearchCase()
}

?效果展示:

本文就介紹到TextArea組件,RichEditor組件,RichText組件以及Search組件了,持續更新中..............

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/77681.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/77681.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/77681.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

除了`String`、`StringBuffer` 和 `StringBuilder`之外,還有什么處理字符串的方法?

一、標準庫中的字符串處理類 1. StringJoiner&#xff08;Java 8&#xff09; 用途&#xff1a;用于在拼接字符串時自動添加分隔符、前綴和后綴。示例&#xff1a;StringJoiner sj new StringJoiner(", ", "[", "]"); sj.add("A").…

Qt中讀寫結構體字節數據

在Qt中讀寫結構體字節數據通常涉及將結構體轉換為字節數組(QByteArray)或直接從內存中讀寫。以下是幾種常見方法&#xff1a; 方法1&#xff1a;使用QDataStream讀寫結構體 cpp #include <QFile> #include <QDataStream>// 定義結構體 #pragma pack(push, 1) //…

Windows 10 上安裝 Spring Boot CLI詳細步驟

在 Windows 10 上安裝 Spring Boot CLI 可以通過以下幾種方式完成。以下是詳細的步驟說明&#xff1a; 1. 手動安裝&#xff08;推薦&#xff09; 步驟 1&#xff1a;下載 Spring Boot CLI 訪問 Spring Boot CLI 官方發布頁面。下載最新版本的 .zip 文件&#xff08;例如 sp…

Unity3D仿星露谷物語開發37之澆水動畫

1、目標 當點擊水壺時&#xff0c;實現澆水的動畫。同時有一個水從水壺中流出來的特效。 假如某個grid被澆過了&#xff0c;則不能再澆水了。。 如果某個grid沒有被dug過&#xff0c;也不能被澆水。 2、優化Settings.cs腳本 增加如下內容&#xff1a; public static float…

【2】Kubernetes 架構總覽

Kubernetes 架構總覽 主節點與工作節點 主節點 Kubernetes 的主節點&#xff08;Master&#xff09;是組成集群控制平面的關鍵部分&#xff0c;負責整個集群的調度、狀態管理和決策。控制平面由多個核心組件構成&#xff0c;包括&#xff1a; kube-apiserver&#xff1a;集…

如何對docker鏡像存在的gosu安全漏洞進行修復——筑夢之路

這里以mysql的官方鏡像為例進行說明&#xff0c;主要流程為&#xff1a; 1. 分析鏡像存在的安全漏洞具體是什么 2. 根據分析結果有針對性地進行修復處理 3. 基于當前鏡像進行修復安全漏洞并復核驗證 # 鏡像地址mysql:8.0.42 安全漏洞現狀分析 dockerhub網站上獲取該鏡像的…

【Tauri2】026——Tauri+Webassembly

前言 不多廢話 直言的說&#xff0c;筆者看到這篇文章大佬的文章 【04】Tauri 入門篇 - 集成 WebAssembly - 知乎https://zhuanlan.zhihu.com/p/533025312嘗試集成一下WebAssembly&#xff0c;直接開始 正文 準備工作 新建一個項目 安裝 vite的rsw插件和rsw pnpm instal…

OpenHarmony Camera開發指導(五):相機預覽功能(ArkTS)

預覽是在相機啟動后實時顯示場景畫面&#xff0c;通常在拍照和錄像前執行。 開發步驟 創建預覽Surface 如果想在屏幕上顯示預覽畫面&#xff0c;一般由XComponent組件為預覽流提供Surface&#xff08;通過XComponent的getXcomponentSurfaceId方法獲取surfaceid&#xff09;&…

puzzle(0531)腦力航跡

目錄 腦力航跡 規則 解法 簡單模式 中等模式 困難模式 專家模式 腦力航跡 規則 2條航跡會產生一個相對航跡&#xff1a; 根據相對航跡和其中一個航跡推導另外一個航跡。 解法 沒有任何需要推理的地方&#xff0c;就是純粹的2個矢量相加。 簡單模式 中等模式 困難模…

在win上安裝Ubuntu安裝Anaconda(linx環境)

一&#xff0c;安裝Ubuntu 1. 在 Microsoft 商城去下載Ubuntu(LTS:是長期維護的版本) 2.安裝完之后啟動程序&#xff0c;再重新打開一個黑窗口&#xff1a; wsl --list --verbose 3.關閉Ubuntu wsl --shutdown Ubuntu-22.04 WSL2 Ubuntu-20.04文件太占c盤空間&#xff0c;…

NEAT 算法解決 Lunar Lander 問題:從理論到實踐

NEAT 算法解決 Lunar Lander 問題:從理論到實踐 0. 前言1. 定義環境2. 配置 NEAT3. 解決 Lunar lander 問題小結系列鏈接0. 前言 在使用 NEAT 解決強化學習問題一節所用的方法只適用于較簡單的強化學習 (reinforcement learning, RL) 環境。在更復雜的環境中使用同樣的進化解…

【KWDB 創作者計劃】_上位機知識篇---ESP32-S3Arduino

文章目錄 前言1. ESP32-S3核心特性2. 開發環境搭建(1) 安裝Arduino IDE(2) 添加ESP32-S3支持(3) 選擇開發板(4) 關鍵配置3. 基礎代碼示例(1) 串口通信(USB/硬件串口)(2) Wi-Fi連接(3) 藍牙LE廣播4. 高級功能開發(1) USB OTG功能(2) AI加速(MicroTensorFlow)(3) 雙核任務處理…

JavaScript學習教程,從入門到精通,DOM節點操作語法知識點及案例詳解(21)

DOM節點操作語法知識點及案例詳解 一、語法知識點 1. 獲取節點 // 通過ID獲取 const element document.getElementById(idName);// 通過類名獲取&#xff08;返回HTMLCollection&#xff09; const elements document.getElementsByClassName(className);// 通過標簽名獲取…

PCA 降維實戰:從原理到電信客戶流失數據應用

一、簡介 在機器學習領域&#xff0c;數據的特征維度往往較高&#xff0c;這不僅會增加計算的復雜度&#xff0c;還可能導致過擬合等問題。主成分分析&#xff08;Principal Component Analysis&#xff0c;簡稱 PCA&#xff09;作為一種經典的降維技術&#xff0c;能夠在保留數…

信創時代編程開發語言選擇指南:國產替代背景下的技術路徑與實踐建議

&#x1f9d1; 博主簡介&#xff1a;CSDN博客專家、CSDN平臺優質創作者&#xff0c;高級開發工程師&#xff0c;數學專業&#xff0c;10年以上C/C, C#, Java等多種編程語言開發經驗&#xff0c;擁有高級工程師證書&#xff1b;擅長C/C、C#等開發語言&#xff0c;熟悉Java常用開…

Arcgis10.1的漢化包及破解文件分享

Arcgis10.1的漢化包分享 網上有好多10.2的漢化包&#xff0c;但是10.1的漢化包很少&#xff0c;特在此分析出來給大家 Arcgis10.1破解文件及漢化包: (訪問密碼: 9784) license manager破解安裝文件 另外也分享了license manager破解安裝文件&#xff0c;也在相同的分享鏈接里…

CrewAI Community Version(一)——初步了解以及QuickStart樣例

目錄 1. CrewAI簡介1.1 CrewAI Crews1.2 CrewAI Flows1.3 Crews和Flows的使用情景 2. CrewAI安裝2.1 安裝uv2.2 安裝CrewAI CLI 3. 官網QuickStart樣例3.1 創建CrewAI Crews項目3.2 項目結構3.3 .env3.4 智能體角色及其任務3.4.1 agents.yaml3.4.2 tasks.yaml 3.5 crew.py3.6 m…

word選中所有的表格——宏

Sub 選中所有表格()Dim aTable As TableApplication.ScreenUpdating FalseActiveDocument.DeleteAllEditableRanges wdEditorEveryoneFor Each aTable In ActiveDocument.TablesaTable.Range.Editors.Add wdEditorEveryoneNextActiveDocument.SelectAllEditableRanges wdEdito…

Tkinter與ttk模塊對比:構建現代 Python GUI 的進化之路

在 Python GUI 開發中&#xff0c;標準庫 tkinter 及其子模塊 ttk&#xff08;Themed Tkinter&#xff09;常被同時使用。本文通過功能對比和實際案例&#xff0c;簡單介紹這兩個模塊的核心差異。 1. 區別 Tkinter&#xff1a;Python 標準 GUI 工具包&#xff08;1994年集成&…

Linux系統之部署Dillinger個人文本編輯器

Linux系統之部署Dillinger個人文本編輯器 一、Dillinger介紹1.1 Dillinger簡介1.2 Dillinger特點1.3 使用場景二、本地環境介紹2.1 本地環境規劃2.2 本次實踐介紹三、檢查本地環境3.1 檢查本地操作系統版本3.2 檢查系統內核版本四、部署Node.js 環境4.1 下載Node.js安裝包4.2 解…