> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/shapes/shape/helper-shapes/lineshape.md).

# LineShape

[SwiftUI](/ios/swiftui.md) ⟩ [Drawing](/ios/swiftui/shapes.md) ⟩ [Shape](/ios/swiftui/shapes/shape.md) ⟩ [Helper Shapes](/ios/swiftui/shapes/shape/helper-shapes.md) ⟩

![LineShape (invisible Shape)](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2FqtoKWmjXwguU9nb8scwu%2FLineShape.png?alt=media\&token=bdb06c07-ace6-4272-8d36-33d3b6837e79)

{% tabs %}
{% tab title="💜 LineShape" %}

````swift
// 2022.02.10

import SwiftUI

/// a line (invisible shape) relative to a rect.
/// ```
/// LineShape([0,0], [1,1])
/// ```
struct LineShape: Shape {
    let p1: UnitPoint
    let p2: UnitPoint
    func path(in rect: CGRect) -> Path {
        Path { path in
            // 🌀Path+ line(), 🅿️ Rectangular
            path.line(rect[p1], rect[p2])
        }
    }
}

// convenience init
extension LineShape {
    init(_ p1: UnitPoint, _ p2: UnitPoint) {
        self.init(p1: p1, p2: p2)
    }
}

````

{% endtab %}

{% tab title="👁️ 預覽" %}

```swift
struct LineShape_Previews: PreviewProvider {
    static var previews: some View {
        HStack {
            Group {
                LineShape([0.5,0], [1,0.5]).stroke()
                LineShape([0.5,1], [1,0.5]).stroke()
                LineShape([0.5,0], [1,1]).stroke()
            }
            .frame(width: 100, height: 100)
            .foregroundColor(.pink)            // ⭐️ set primary color
            .border(Color.primary.opacity(0.4))
        }
        .padding()
    }
}
```

{% endtab %}

{% tab title="👥 相關" %}

* is an (invisible) [Shape](/ios/swiftui/shapes/shape.md).
* used by [Line](/ios/swiftui/shapes/shape/helper-shapes/line.md) (a [View](/ios/swiftui/view/view.md)).
  {% endtab %}
  {% endtabs %}
