【发布时间】:2021-01-23 22:36:21
【问题描述】:
我正在使用 SwiftUI 为 iOS、iPadOS 和 macOS 构建应用程序,我需要一个具有透明背景的 TextEditor。
之前有人问过这个问题,我找到了适用于 iOS/iPadOS (Change background color of TextEditor in SwiftUI) 的好答案,但没有找到适用于 macOS 的答案。
这是我基于上面链接的代码,非常适用于 iOS 和 iPadOS:
TextEditor(text: $text)
.foregroundColor(.black)
.onAppear {
#if os(macOS)
// ??
#else
UITextView.appearance().backgroundColor = .clear
#endif
}
NSTextView 似乎与UITextView.appearance() 没有等效项,所以我不完全确定在那里做什么。
使用.background(Color.clear) 也不起作用:
TextEditor(text: $text)
.foregroundColor(.black)
.background(Color.clear)
.onAppear {
#if os(macOS)
// ??
#else
UITextView.appearance().backgroundColor = .clear
#endif
}
有人知道在 macOS 上为 TextEditor 获取透明背景的方法吗?
此外,已经在此处专门针对 macOS 提出了这个问题:Changing TextEditor background color in SwiftUI for macOS,但没有给出有效的答案。
【问题讨论】:
标签: swift macos swiftui nstextview