harmony 鸿蒙@ohos.measure (Text Measurement)

2023-06-24 浏览 (775)

@ohos.measure (Text Measurement)

The measure module provides APIs for measuring text metrics, such as text height and width.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import measure from '@ohos.measure'

measure.measureText

measureText(options: MeasureOptions): number

Measures the width of the given single-line text.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsMeasureOptionsYesInformation about the measured text.

Return value

TypeDescription
numberText width.
The unit is px.

Example

import measure from '@ohos.measure'
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  @State textWidth : number = measure.measureText({
    textContent: "Hello word",
    fontSize: '50px'
  })
  build() {
    Row() {
      Column() {
        Text("The width of 'Hello World': " + this.textWidth)
      }
      .width('100%')
    }
    .height('100%')
  }
}

measure.measureTextSize10+

measureTextSize(options: MeasureOptions): SizeOptions

Measures the width of the given single-line text.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsMeasureOptionsYesInformation about the measured text.

Return value

TypeDescription
SizeOptionLayout width and height occupied by the text.
The unit is px.

Example

import measure from '@ohos.measure'
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  textSize : SizeOptions = measure.measureTextSize({
    textContent: "Hello word",
    fontSize: '50px'
  })
  build() {
    Row() {
      Column() {
        Text("The width of 'Hello World': " + this.textSize.width)
        Text("The height of 'Hello World': " + this.textSize.height)
      }
      .width('100%')
    }
    .height('100%')
  }
}

MeasureOptions

Provides attributes of the measured text.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
textContentstringYesContent of the measured text.
constraintWidth10+number |string |ResourceNoLayout width of the measured text.
The default unit is vp.
fontSizenumber |string |ResourceNoFont size of the measured text. If the value is of the number type, the unit fp is used.
Default value: 16fp
NOTE
The value cannot be a percentage.
fontStylenumber |FontStyleNoFont style of the measured text.
Default value: FontStyle.Normal
fontWeightnumber |string |FontWeightNoFont width of the measured text.
For the number type, the value ranges from 100 to 900, at an interval of 100. A larger value indicates a heavier font weight. The default value is 400.
For the string type, only strings of the number type are supported, for example, 400, "bold", "bolder", "lighter", "regular", and "medium", which correspond to the enumerated values in FontWeight.
Default value: FontWeight.Normal
fontFamilystring |ResourceNoFont family of the measured text. Default value: 'HarmonyOS Sans'
Only the default font is supported.
letterSpacingnumber |stringNoLetter spacing of the measured text.
textAlign10+number |TextAlignNoHorizontal alignment mode of the measured text.
Default value: TextAlign.Start
overflow10+number |TextOverflowNoDisplay mode when the measured text is too long.
maxLines10+numberNoMaximum number of lines in the measured text.
lineHeight10+number |string |ResourceNoLine height of the measured text.
baselineOffset10+number |stringNoBaseline offset of the measured text.
Default value: 0
textCase10+number |TextCaseNoCase of the measured text.
Default value: TextCase.Normal

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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