用户可快速实现 OpenHarmony 应用中文本样式定制。该工具支持字体间距、颜色、布局及富文本高亮等设置,提供点击事件回调,提升通知类等场景开发效率。【此简介由AI生成】
Access rate limit exceeded. Please log in to continue.
TextLayoutBuilder
Introduction
This library is adapted based on the source library TextLayoutBuilder to run on OpenHarmony. TextLayoutBuilder is a tool for building text layouts, including the font spacing, size, color, layout mode, and rich text highlighting. It is used in all text display scenarios, especially when the font style is specially displayed for notifications, greatly improving the development efficiency.
Effect

How to Install
ohpm install @ohos/textlayoutbuilder
For details about the OpenHarmony ohpm environment configuration, see OpenHarmony HAR.
How to Use
Step 1: Import the TextLayout component to your project and instantiate the TextLayout.Layout object.
...
import TextLayout from '@ohos/textlayoutbuilder';
import {TextInfo} from '@ohos/textlayoutbuilder';
...
@State layout:TextLayout.Layout = new TextLayout.Layout();
...
Step 2:: Set UI attributes through the layout class object to define the required style. You can also add required callbacks.
private aboutToAppear() {
let textInfo = new TextInfo();
textInfo.setStart(2)
textInfo.setEnd(8)
textInfo.setFontColor('#ff0000')
... ...
this.layout
.setText(this.strTest)
.setSpecialTextInfo(textInfo1)
.setSpecialTextInfo(textInfo2)
.setSpecialTextInfo(textInfo3)
.setSpecialTextClick((textInfo) =>{
console.info ('I clicked = '+textInfo.getText())
});
}
Step 3: Draw a page and transfer the customized layout to TextLayout.
build() {
Column() {
Text ("Displayed by default").fontSize(16).fontColor("#999999").margin({ left: 14, top: 14, bottom: 14 })
Column() {
TextLayout({ model: this.layout })
}.backgroundColor("#cccccc").margin({ left: 14, right: 14 }).borderRadius(10)
}.alignItems(HorizontalAlign.Start)
}
Available APIs
let layout:TextLayout.Layout = new TextLayout.Layout();
-
Set the text content using layout.setText(text: string).
Parameters
Parameter Type Mandatory Description text string Yes Text content -
Set the specified text style using layout.setSpecialTextInfo(specialTextInfo: TextInfo).
Parameters
Parameter Type Mandatory Description specialTextInfo TextInfo Yes Text content style -
Set the text color using layout.setTextColor(textColor: string.|Color|number)
Parameters
Parameter Type Mandatory Description textColor string|Color|number Yes Text color -
Set whether to display in a single line using layout.setSingleLine(singleLine: boolean).
Parameters
Parameter Type Mandatory Description singleLine boolean Yes Whether to display in a single line. The default value is No. -
Set the maximum number of lines using layout.setMaxLines(maxLines: number).
Parameters
Parameter Type Mandatory Description maxLines number Yes Maximum number of lines to set. -
Set the ellipsis replacement mode using layout.setEllipsize(ellipsize: TextOverflow) when the text cannot be completely displayed.
Parameters
Parameter Type Mandatory Description ellipsize TextOverflow Yes Text overflow display mode. -
Set the text alignment mode using layout.setAlignment(textalign: TextAlign).
Parameters
Parameter Type Mandatory Description textalign TextAlign Yes Text alignment mode. -
Set the minimum number of characters displayed in each line using layout.setMinEms(minEms: number).
Parameters
Parameter Type Mandatory Description minEms number Yes Number of characters displayed in each line. -
Set whether to set the inner margin of the component using layout.setIncludeFontPadding(shouldInclude: boolean).
Parameters
Parameter Type Mandatory Description shouldInclude boolean Yes Whether to set padding. The default value is No. -
Setting the maximum width using layout.setMaxWidth(maxWidth: number).
Parameters
Parameter Type Mandatory Description maxWidth number Yes Maximum width -
Set whether to enable the status change function using layout.setIsEnablePressState(isEnablePressState: boolean) when the text is touched.
Parameters
Parameter Type Mandatory Description isEnablePressState boolean Yes Whether to enable the status change function when the text is pressed. By default, the function is disabled. -
Set the status style of the pressed text using layout.setTextPressStateStyle(textPressStateStyle: string).
Parameters
Parameter Type Mandatory Description textPressStateStyle string Yes Style of the pressed text status -
Set the click event of the specified text using layout.setSpecialTextClick(clickCallback: (textInfo: TextInfo) => void).
Parameters
Parameter Type Mandatory Description clickCallback (textInfo: TextInfo) => void Yes Custom callback.
Set special character attributes using let textInfo = new TextInfo().
-
Set the start position of the special text using textInfo.setStart(start:number).
Parameters
Parameter Type Mandatory Description start number Yes Start position of the special text. -
Set the end position of the special text using textInfo.setEnd(end:number).
Parameters
Parameter Type Mandatory Description end number Yes End position of the special text. -
Set the text color using textInfo.setFontColor(fontColor:string).
Parameters
Parameter Type Mandatory Description fontColor string Yes Font color. -
Set the text content using textInfo.setText(text:string).
Parameters
Parameter Type Mandatory Description text string Yes Text content. -
Set the text type using textInfo.setTextType(textType:string).
Parameters
Parameter Type Mandatory Description textType string Yes Text type (TEXT_TYPE_NORMAL|TEXT_TYPE_RICH|TEXT_TYPE_HTTP). -
Set the distance between text fonts using textInfo.setFontLetterSpacing(fontLetterSpacing:string).
Parameters
Parameter Type Mandatory Description fontLetterSpacing string Yes Distance between text fonts. -
Set the font size using textInfo.setFontSize(fontSize:string).
Parameters
Parameter Type Mandatory Description fontSize string Yes Font size. -
Set the font style using textInfo.setFontStyle(fontStyle:string).
Parameters
Parameter Type Mandatory Description fontStyle string Yes Font style.
Constraints
This project has been verified in the following version:
-
DevEco Studio: NEXT Release(5.0.3.900), SDK: API12 (5.0.0.71)
-
DevEco Studio: 3.1 Beta2 (3.1.0.400), SDK: API 9 Release (3.2.11.9)
Directory Structure
|---- TextLayoutBuilder
| |---- entry # Sample code
| |---- library # TextLayout library
| |---- src
| |---- main
| |---- ets
| |---- commonents
| |---- TextInfo.ets # Rich text data entity
| |---- TextLayout.ets # UI custom component
| |---- index.ets # External APIs
| |---- README.md # Readme
About obfuscation
- Code obfuscation, please seeCode Obfuscation
- If you want the ohos_text_layout_builder library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/textlayoutbuilder
How to Contribute
If you find any problem during the use, submit an Issue or a PR to us.
License
This project is licensed under Apache License 2.0.
Introduction
用户可快速实现 OpenHarmony 应用中文本样式定制。该工具支持字体间距、颜色、布局及富文本高亮等设置,提供点击事件回调,提升通知类等场景开发效率。【此简介由AI生成】
Customize your domain