1# Line 2 3The **\<Line>** component is used to draw a straight line. 4 5> **NOTE** 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9## Child Components 10 11Not supported 12 13 14## APIs 15 16Line(value?: {width?: string | number, height?: string | number}) 17 18Since API version 9, this API is supported in ArkTS widgets. 19 20**Parameters** 21 22| Name| Type| Mandatory| Default Value| Description| 23| -------- | -------- | -------- | -------- | -------- | 24| width | string \| number | No| 0 | Width.| 25| height | string \| number | No| 0 | Height.| 26 27 28## Attributes 29 30In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 31 32| Name| Type| Default Value| Description| 33| -------- | -------- | -------- | -------- | 34| startPoint | Array<Length> | [0, 0] | Coordinates (relative coordinates) of the start point of the line, in vp.<br>Since API version 9, this API is supported in ArkTS widgets.| 35| endPoint | Array<Length> | [0, 0] | Coordinates (relative coordinates) of the end point of the line, in vp.<br>Since API version 9, this API is supported in ArkTS widgets.| 36| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | Color of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a closed shape.| 37| fillOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Opacity of the fill area.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a closed shape.| 38| stroke | [ResourceColor](ts-types.md) | - | Stroke color. If this attribute is not set, the component does not have any stroke.<br>Since API version 9, this API is supported in ArkTS widgets.| 39| strokeDashArray | Array<Length> | [] | Stroke dashes.<br>Since API version 9, this API is supported in ArkTS widgets.| 40| strokeDashOffset | number \| string | 0 | Offset of the start point for drawing the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.| 41| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | Cap style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.| 42| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | Join style of the stroke.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not work for the **\<Line>** component, which does not have corners.| 43| strokeMiterLimit | number \| string | 4 | Limit value when the sharp angle is drawn as a miter.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>This attribute does not take effect because the **\<Line>** component cannot be used to draw a shape with a sharp angle.| 44| strokeOpacity | number \| string \| [Resource](ts-types.md#resource)| 1 | Stroke opacity.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>The value range is [0.0, 1.0]. If the set value is less than 0.0, **0.0** will be used. If the set value is greater than 1.0, **1.0** will be used.| 45| strokeWidth | Length | 1 | Stroke width.<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>The value cannot be a percentage.| 46| antiAlias | boolean | true | Whether anti-aliasing is enabled.<br>Since API version 9, this API is supported in ArkTS widgets.| 47 48## Example 49 50### Example 1 51 52```ts 53// xxx.ets 54@Entry 55@Component 56struct LineExample { 57 build() { 58 Column({ space: 10 }) { 59 // The coordinates of the start and end points of the line are determined relative to the coordinates of the drawing area of the <Line> component. 60 Line() 61 .width(200) 62 .height(150) 63 .startPoint([0, 0]) 64 .endPoint([50, 100]) 65 .stroke(Color.Black) 66 .backgroundColor('#F5F5F5') 67 Line() 68 .width(200) 69 .height(150) 70 .startPoint([50, 50]) 71 .endPoint([150, 150]) 72 .strokeWidth(5) 73 .stroke(Color.Orange) 74 .strokeOpacity(0.5) 75 .backgroundColor('#F5F5F5') 76 // strokeDashOffset is used to define the offset when the associated strokeDashArray array is rendered. 77 Line() 78 .width(200) 79 .height(150) 80 .startPoint([0, 0]) 81 .endPoint([100, 100]) 82 .stroke(Color.Black) 83 .strokeWidth(3) 84 .strokeDashArray([10, 3]) 85 .strokeDashOffset(5) 86 .backgroundColor('#F5F5F5') 87 // If the coordinates of a point are beyond the width and height range of the <Line> component, the line will exceed the drawing area. 88 Line() 89 .width(50) 90 .height(50) 91 .startPoint([0, 0]) 92 .endPoint([100, 100]) 93 .stroke(Color.Black) 94 .strokeWidth(3) 95 .strokeDashArray([10, 3]) 96 .backgroundColor('#F5F5F5') 97 } 98 } 99} 100``` 101 102 103 104### Example 2 105 106```ts 107// xxx.ets 108@Entry 109@Component 110struct LineExample1 { 111 build() { 112 Row({ space: 10 }) { 113 // Set LineCapStyle to Butt. 114 Line() 115 .width(100) 116 .height(200) 117 .startPoint([50, 50]) 118 .endPoint([50, 200]) 119 .stroke(Color.Black) 120 .strokeWidth(20) 121 .strokeLineCap(LineCapStyle.Butt) 122 .backgroundColor('#F5F5F5').margin(10) 123 // Set LineCapStyle to Round. 124 Line() 125 .width(100) 126 .height(200) 127 .startPoint([50, 50]) 128 .endPoint([50, 200]) 129 .stroke(Color.Black) 130 .strokeWidth(20) 131 .strokeLineCap(LineCapStyle.Round) 132 .backgroundColor('#F5F5F5') 133 // Set LineCapStyle to Square. 134 Line() 135 .width(100) 136 .height(200) 137 .startPoint([50, 50]) 138 .endPoint([50, 200]) 139 .stroke(Color.Black) 140 .strokeWidth(20) 141 .strokeLineCap(LineCapStyle.Square) 142 .backgroundColor('#F5F5F5') 143 } 144 } 145} 146``` 147 148 149 150### Example 3 151 152```ts 153// xxx.ets 154@Entry 155@Component 156struct LineExample { 157 build() { 158 Column() { 159 Line() 160 .width(300) 161 .height(30) 162 .startPoint([50, 30]) 163 .endPoint([300, 30]) 164 .stroke(Color.Black) 165 .strokeWidth(10) 166 // Set the interval for strokeDashArray to 50. 167 Line() 168 .width(300) 169 .height(30) 170 .startPoint([50, 20]) 171 .endPoint([300, 20]) 172 .stroke(Color.Black) 173 .strokeWidth(10) 174 .strokeDashArray([50]) 175 // Set the interval for strokeDashArray to 50, 10. 176 Line() 177 .width(300) 178 .height(30) 179 .startPoint([50, 20]) 180 .endPoint([300, 20]) 181 .stroke(Color.Black) 182 .strokeWidth(10) 183 .strokeDashArray([50, 10]) 184 // Set the interval for strokeDashArray to 50, 10, 20. 185 Line() 186 .width(300) 187 .height(30) 188 .startPoint([50, 20]) 189 .endPoint([300, 20]) 190 .stroke(Color.Black) 191 .strokeWidth(10) 192 .strokeDashArray([50, 10, 20]) 193 // Set the interval for strokeDashArray to 50, 10, 20, 30. 194 Line() 195 .width(300) 196 .height(30) 197 .startPoint([50, 20]) 198 .endPoint([300, 20]) 199 .stroke(Color.Black) 200 .strokeWidth(10) 201 .strokeDashArray([50, 10, 20, 30]) 202 203 } 204 } 205} 206``` 207 208 209