[Daily HarmonyOS Next Knowledge] Input Box Focus Color, Text Overflow Handling, Device-Aware Screen Rotation, Basic Control Encapsulation, Global Font Setting
1. Does HarmonyOS support modifying the focus cursor color for input boxes?
- RichEditor: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-richeditor-V5#ZH-CN_TOPIC_0000001930676557__caretcolor12
- TextInput: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-textinput-V5
- Search: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-search-V5
2. How to display an "ellipsis + more" when text exceeds the limit in HarmonyOS?
Control text overflow with overflow: TextOverflow.Ellipsis.
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-text-V5#textoverflow
textOverflow(value: { overflow: TextOverflow })
- Sets the display mode for overflow text, truncating by character (e.g., words for English). To truncate by letter, use zero-width spaces (
\u200B). From API version 11, combine withwordBreak: WordBreak.BREAK_ALLfor letter-level truncation. - When
overflowis set toTextOverflow.None,TextOverflow.Clip, orTextOverflow.Ellipsis,maxLinesmust be configured; otherwise, it fails.TextOverflow.NoneandTextOverflow.Cliphave the same effect. - When
overflowis set toTextOverflow.MARQUEE, text scrolls in a single line.maxLinesandcopyOptionare ignored.textAlignworks only when text is non-scrollable. Theclipproperty defaults totruein marquee mode, andCustomSpanin attribute strings is unsupported.
3. How to enable screen rotation based on device settings in HarmonyOS?
Dynamically detect device types and configure auto-rotation support: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setpreferredorientation9
setPreferredOrientation(orientation: Orientation, callback: AsyncCallback<void>): void
- Sets the main window's display orientation with an async callback. Effective only on devices supporting sensor-driven rotation, not on 2-in-1 devices, free multi-window mode, or sub-windows.
Use device information APIs to distinguish device types and enable auto-rotation accordingly:
Reference: @ohos.distributedDeviceManager (Device Management): https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-distributeddevicemanager-V5
Key capabilities:
- Register/unregister device online/offline listeners.
- Discover nearby untrusted devices.
- Authenticate/取消认证设备.
- Query trusted device lists.
- Retrieve local device info (name, type, ID, etc.).
4. How to encapsulate commonly used basic controls (e.g., Button, Text, Column) in HarmonyOS projects?
Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-create-custom-components-V5
In ArkUI, UI elements are components: system-provided or developer-defined (custom components). Custom components enhance code reusability, separate business logic from UI, and facilitate version evolution.
Key features:
- Composability: Combine system components, properties, and methods.
- Reusability: Reuse custom components as instances in different parent components/containers.
- Data-driven UI update: Modify state variables to trigger UI refreshes.
5. How to set global fonts in HarmonyOS?
-
Text components: Use
Text.Font.familyfor custom fonts. Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-types-V5#font -
Canvas: Does not support custom fonts. Use
RenderingContextSettings.fontwith supported families (e.g.,'sans-serif','serif','monospace'). Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-canvasrenderingcontext2d-V5
Top comments (0)