惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
L
LangChain Blog
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
V
V2EX

Super Blog

[译] 我所知道的全部智能体工程技巧(2026 年 6 月) 2024:悲观者正确,乐观者前行 Overleaf LaTeX citation 无法正常显示问题解决 Mac 终端无法联网问题解决 我和我的 2023 新起点 使用 Arc 浏览器自定义网页 我和我的 2022 Genius Bar 两日游 实训小记 《软件测试技术》笔记 Q-Learning 简介 软件测试技术实验 3 和 4 遇到的一些问题和解决方案 我和我的 2021 写过的第二个 App 关于最近,以及…… 《数据库原理》笔记 《数字逻辑与数字系统》笔记 《形式化方法》笔记 宣言
在 SwiftUI 中自定义修饰器
SUPER · 2021-07-13 · via Super Blog

学习 swiftUI 的记录。

首先定义结构体,在其中对内容修饰:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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,在其中定义修饰器函数:

1
2
3
4
5
extension View {
func watermarked(with text: String) -> some View {
self.modifier(Watermark(text: text))
}
}

最终只需调用 extension 中的函数即可实现:

1
2
3
Color.green
.frame(width: 300, height: 200)
.watermarked(with: "Hacking with Swift")

效果图:

image-20210713104903030