openharmony 鸿蒙 simple-text-arkts

2026-08-25 浏览 (1)

Drawing and Displaying Simple Text (ArkTS)

Overview

In a simple user interface, only a few lines of static text need to be displayed, such as the text on a label, button, menu item, or status bar. In this case, you only need to select a proper font, size, and color to complete the rendering.

Related Attributes

In this scenario, the following text style attributes are involved. For details, see TextStyle.

  • color: font color. The default value is white. Note that the font color must be different from the canvas color to ensure that the text can be properly displayed.

  • fontSize: font size, in units of px. The value is a floating point number. The default value is 14.0.

How to Develop

  1. Obtain the canvas object through context.

    let canvas = context.canvas;
    
  2. Initialize the text style. Set the font color to red and font size to 50.

    // Obtain the text style.
    let myTextStyle: text.TextStyle = {
      // Text color.
      color: {
        alpha: 255,
        red: 255,
        green: 0,
        blue: 0
      },
      // Text size.
      fontSize: 100
    };
    
  3. Initialize the paragraph style.

    let myParagraphStyle: text.ParagraphStyle = {
      textStyle: myTextStyle,
    };
    
  4. Initialize the paragraph object and add text.

    let fontCollection = text.FontCollection.getGlobalInstance();
    let ParagraphGraphBuilder = new text.ParagraphBuilder(myParagraphStyle, fontCollection);
    // Push the text style.
    ParagraphGraphBuilder.pushStyle(myTextStyle);
    // Add text.
    ParagraphGraphBuilder.addText("Hello World");
    
  5. Layout the paragraph and draw the text.

    // Generate a paragraph.
    let paragraph = ParagraphGraphBuilder.build();
    // Layout.
    paragraph.layoutSync(1250);
    // Draw the text.
    paragraph.paint(canvas, 0, 100);
    

Effect

image_0000002246603717

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 text-overview

openharmony 鸿蒙 complex-text-c

openharmony 鸿蒙 geometric-shape-drawing-arkts

openharmony 鸿蒙 native-fence-guidelines

openharmony 鸿蒙 native-vsync-guidelines

openharmony 鸿蒙 displaysync-overview

openharmony 鸿蒙 custom-font-c

openharmony 鸿蒙 system-font-c

openharmony 鸿蒙 canvas-get-result-draw-arkts

openharmony 鸿蒙 primitive-drawing-overview

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/AY9clpUd