js-components-basic-label.md 6.4 KB
Newer Older
E
ester.zhou 已提交
1
# label
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7 8 9
The **\<label>** component defines labels for the **\<input>**, **\<button>**, and **\<textarea>** components. When a label is clicked, the click effect of the component associated with the label will be triggered.

## Required Permissions
Z
zengyawen 已提交
10 11 12

None

E
ester.zhou 已提交
13 14

## Child Components
Z
zengyawen 已提交
15 16 17 18

Not supported


E
ester.zhou 已提交
19 20 21 22 23 24 25 26
## Attributes

In addition to the [universal attributes](../arkui-js/js-components-common-attributes.md), the following attributes are supported.

| Name    | Type    | Default Value | Mandatory  | Description         |
| ------ | ------ | ---- | ---- | ----------- |
| target | string | -    | No   | Attribute ID of the target component.|

Z
zengyawen 已提交
27

E
ester.zhou 已提交
28
## Styles
Z
zengyawen 已提交
29

E
ester.zhou 已提交
30
In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported.
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Name               | Type                        | Default Value       | Mandatory  | Description                                      |
| ----------------- | -------------------------- | ---------- | ---- | ---------------------------------------- |
| color             | &lt;color&gt;              | #e5000000  | No   | Font color.                                |
| font-size         | &lt;length&gt;             | 30px       | No   | Font size.                                |
| allow-scale       | boolean                    | true       | No   | Whether the font size changes with the system's font size settings.<br>For details about how to make the configuration take effect dynamically, see the **config-changes** attribute in the **config.json** file.|
| letter-spacing    | &lt;length&gt;             | 0px        | No   | Character spacing (px).                              |
| font-style        | string                     | normal     | No   | Font style. Available values are as follows:<br>- **normal**: standard font style<br>- **italic**: italic font style|
| font-weight       | number \| string | normal     | No   | Font width. For the number type, the value ranges from 100 to 900. The default value is 400. A larger value indicates a larger font width.<br>The value of the number type must be an integer multiple of 100.<br>The value of the string type can be **lighter**, **normal**, **bold**, or **bolder**.|
| text-decoration   | string                     | none       | No   | Text decoration. Available values are as follows:<br>- **underline**: An underline is used.<br>- **line-through**: A strikethrough is used.<br>- **none**: The standard text is used.|
| text-align        | string                     | start<br>| No   | Text alignment mode. Available values are as follows:<br>- **left**: The text is left-aligned.<br>- **center**: The text is center-aligned.<br>- **right**: The text is right-aligned.<br>- **start**: The text is aligned with the direction in which the text is written.<br>- **end**: The text is aligned with the opposite direction in which the text is written.<br>If the text width is not specified, the alignment effect may not be obvious when the text width is the same as the width of the parent container.|
| line-height       | &lt;length&gt;             | 0px        | No   | Text line height. When this parameter is set to **0px**, the text line height is not limited and the font size is adaptive.      |
| text-overflow     | string                     | clip       | No   | Takes effect when the maximum number of lines is specified. Available values are as follows:<br>- **clip**: The text is clipped and displayed based on the size of the parent container.<br>- **ellipsis**: The text is displayed based on the size of the parent container. The text that cannot be displayed is replaced with ellipsis. This style must be used together with **max-lines**.|
| font-family       | string                     | sans-serif | No   | Font family, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font in the family or the specified [custom font](../arkui-js/js-components-common-customizing-font.md) is used for the text.|
| max-lines         | number                     | -          | No   | Maximum number of lines in the text.                              |
| min-font-size     | &lt;length&gt;             | -          | No   | Minimum font size in the text. This style must be used together with **max-font-size**. The font size can be changed dynamically. After the maximum and minimum font sizes are set, **font-size** does not take effect.|
| max-font-size     | &lt;length&gt;             | -          | No   | Maximum font size in the text. This style must be used together with **min-font-size**. The font size can be changed dynamically. After the maximum and minimum font sizes are set, **font-size** does not take effect.|
| font-size-step    | &lt;length&gt;             | 1px        | No   | Step for dynamically adjusting the font size in the text. The minimum and maximum font sizes must be set.           |
| prefer-font-sizes | &lt;array&gt;              | -          | No   | Preset preferred font sizes. For dynamic font size adjustment, the preset sizes are used to match the maximum number of lines in the text. If the preferred font sizes were not set, the font size will be adjusted based on the maximum and minimum font sizes and the step you have set. If the maximum number of lines in the text cannot be met, **text-overflow** is used to truncate the text. If this parameter is set, **font-size**, **max-font-size**, **min-font-size**, and **font-size-step** do not take effect.<br>Example values: **12px,14px,16px**|
Z
zengyawen 已提交
50 51


E
ester.zhou 已提交
52
## Events
Z
zengyawen 已提交
53 54 55

Not supported

E
ester.zhou 已提交
56 57

## Methods
Z
zengyawen 已提交
58 59 60 61

Not supported


E
ester.zhou 已提交
62 63 64
## Example

```html
Z
zengyawen 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<!--xxx.hml -->
<div class="container">
  <div class="row">
    <label class="label" target="textId">input</label>
    <input class="input" id="textId" type="text"></input>
  </div>
  <div class="row">
    <label class="label" target="radioId">radio</label>
    <input class="input" id="radioId" type="radio" name="group" value="group"></input>
  </div>
  <div class="row">
    <label class="label" target="checkboxId">checkbox</label>
    <input class="input" id="checkboxId" type="checkbox"></input>
  </div>
</div>
```

E
ester.zhou 已提交
82
```css
Z
zengyawen 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/*xxx.css */
.container {
  flex-direction: column;
  align-items: center;
}
.row {
  flex-direction: row;
}
.label {
  width: 200px;
  margin-top: 50px;
}
.input {
  margin-left: 100px;
  margin-top: 50px;
}
```

E
ester.zhou 已提交
101
![en-us_image_0000001152834002](figures/en-us_image_0000001152834002.png)