๐StrokeStyle
Last updated
Last updated
cap : ็ท้ ญ็้จๅ
join : ็ท่ฝๅฝๆ็้ฃๆฅๆนๅผ
dash : ่็ทๆจฃๅผ
width : ็ทๅฏฌ
import SwiftUI
public extension StrokeStyle {
/// # Line Cap
/// ็ท้ ญ็่กจ็พๆนๅผ๏ผ
/// - `.butt`๏ผๅนณ้ ญใ(โญ๏ธ ้ ่จญๅผ)
/// - `.round`๏ผๅ้ ญใ
/// - `.square`๏ผๆน้ ญใ
/// ## โ ๏ธ ๆณจๆ๏ผ
/// - ้คไบ `.butt` ไนๅค๏ผๅ
ถไปๅ
ฉ็จฎ้ฝๆ็ชๅบ็ท้ ญๅคโผ๏ธ
/// - Parameters:
/// - cap: ็ท้ ญ็่กจ็พๆนๅผ๏ผๆ `.butt`ใ`.round` ๆ `.square` ไธ็จฎใ
public func lineCap(_ cap: CGLineCap) -> StrokeStyle {
var style = self
style.lineCap = cap
return style
}
/// # Line Join
/// ็ท็้ๆฅๆนๅผ๏ผ
/// - `.miter`๏ผ็จใๅฐ่งใ็ๆนๅผ้ๆฅใ(โญ๏ธ ้ ่จญๅผ)
/// - `.bevel`๏ผๆๅฐ่งใๅๅนณใใ
/// - `.round`๏ผ็จใๅ่งใ็ๆนๅผใ
/// - Parameters:
/// - join: ็ท็้ๆฅๆนๅผ๏ผๆ `.miter`ใ`.bevel`ใ`.round` ไธ็จฎใ
public func lineJoin(_ join: CGLineJoin) -> StrokeStyle {
var style = self
style.lineJoin = join
return style
}
/// # Line Width
public func lineWidth(_ width: CGFloat) -> StrokeStyle {
var style = self
style.lineWidth = width
return style
}
/// # Dash
/// ่็ท็ๆจฃๅผใไพๅฆ๏ผ
/// - `[10, 5]`๏ผไปฃ่กจ `10 points` ็ซ็ท๏ผ`5 points` ็ฉบ็ฝใ
/// - `style.dash([10, 5])`
public func dash(_ dash: [CGFloat]) -> StrokeStyle {
var style = self
style.dash = dash
return style
}
/// Example: `style.dash(10, 5)`
public func dash(_ dash: CGFloat...) -> StrokeStyle {
self.dash(dash)
}
}
โฌ๏ธ ้่ฆ๏ผ Vector2D, .watermark()
import SwiftUI
// ่จญ่จๅๅฐบๅฏธ
let s: CGFloat = 100 // ้้ท
let w: CGFloat = 40 // ็ทๅฏฌ
let d = s + w // ็ธฝๅฏฌๅบฆ = ้้ท + ็ท้ ญๅฏ่ฝ็ชๅบ็้จๅ
// ่จญ่จๅ้ ้ป
let p1 = CGPoint(w, w) * 0.5 // ่ตท้ป // โญ๏ธ required: ๐
ฟ๏ธ Vector2D
let p2 = p1 + [s, 0] // ่ฝๆ้ป
let p3 = p2 + [0, s] // ็ต้ป
// ่จญ่จๅ็ซ็ทๆจฃๅผ (ๅ
่จญๅฎๅฏฌๅบฆ)
let style = StrokeStyle().lineWidth(w)
// ่จญ่จๅ็ทๆข่ทฏ็ท
let path = Path { p in
p.move (to: p1)
p.addLine(to: p2)
p.addLine(to: p3)
}
// ่จญ่จๅ
struct LineStyle: View {
// ๅฏ่จญๅฎใ็ทๆขๆจฃๅผใ
let style: StrokeStyle
// LineStyle(strokeStyle)
init(_ s: StrokeStyle) { self.style = s }
// ่จญ่จๅๅ
งๅฎน
var body: some View {
ZStack {
// 1. ๅ
ๅฐ่จญ่จ่ทฏ็ทไปฅใ็ทๆขๅ ๅฏฌๆจฃๅผใๅ ้็ท๏ผ็ถๅพ่่ฒใ
path
.strokedPath(style) // โญ๏ธ still a `Path`
.foregroundColor(.pink)
.frame(width: d, height: d)
// 2. ็ซใ่จญ่จๅ่ทฏ็ทใๆฌ่บซ
path
.stroke() // โญ๏ธ a `Shape`
.foregroundColor(.white)
.frame(width: d, height: d)
// 3. ๆๅบใๅ ๅฏฌ็้็ทใ
path
.strokedPath(style)
.stroke(lineWidth: 2)
.foregroundColor(.black)
.frame(width: d, height: d)
} // container (ZStack)
.background(Rectangle().opacity(0.6))
}
}
struct ContentView: View {
var body: some View {
VStack {
// Line Join
Text("Line Join").fontWeight(.bold)
HStack {
LineStyle(style.lineJoin(.miter)) // โญ๏ธ default
.watermark("โญ๏ธmiter", .bottomLeading)
LineStyle(style.lineJoin(.bevel))
.watermark("bevel", .bottomLeading)
LineStyle(style.lineJoin(.round))
.watermark("round", .bottomLeading)
}
// Line Cap
Text("Line Cap").fontWeight(.bold)
HStack {
LineStyle(style.lineCap(.butt)) // โญ๏ธ default
.watermark("โญ๏ธbutt", .bottomLeading)
LineStyle(style.lineCap(.square))
.watermark("square", .bottomLeading)
LineStyle(style.lineCap(.round))
.watermark("round", .bottomLeading)
}
} // container (VStack)
.padding()
.background(Color.gray)
.shadow(color: Color.black.opacity(0.8), radius: 4, x: 4, y: 4)
}
}
SwiftUI โฉ StrokeStyle