3
在 swiftUI 中自定义修饰器
source link: https://blog.superpung.xyz/custom-modifier/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在 swiftUI 中自定义修饰器
Posted on
2021-07-13
Views: 17
Symbols count in article:
560
Reading time ≈
1 mins.
学习 swiftUI 的记录。
首先定义结构体,在其中对内容修饰:
struct Watermark: ViewModifier {
var text: String
func body(content: Content) -> some View {
ZStack(alignment: .bottomTrailing) {
content
Text(text)
.font(.caption)
.foregroundColor(.white)
.padding(5)
.background(Color.black)
}
}
}
然后根据此结构体,定义 extension
,在其中定义修饰器函数:
extension View {
func watermarked(with text: String) -> some View {
self.modifier(Watermark(text: text))
}
}
最终只需调用 extension
中的函数即可实现:
Color.green
.frame(width: 300, height: 200)
.watermarked(with: "Hacking with Swift")
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK