英文:
Is there a way to make the textfield appear in different color without affecting the semi-transparent background?
问题
以下是您要翻译的内容:
我在想是否可以更改蓝色边框内文本字段的颜色,而不更改半透明的周围区域。这是我拥有的屏幕截图:
这是我的代码:
import SwiftUI
struct ContentView: View {
@State private var textFieldContent = ""
var body: some View {
ZStack {
VisualEffectView()
GeometryReader { geometry in
VStack {
Rectangle()
.foregroundColor(Color(red: 0.1, green: 0.1, blue: 0.1))
.frame(height: geometry.size.height / 1.16)
Rectangle()
.foregroundColor(.clear)
.frame(height: geometry.size.height * 2 / 3)
}
HStack {
TextField("Send a message", text: $textFieldContent)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.clear)
.cornerRadius(25)
.focusable(false)
.padding(.leading, 20)
Spacer()
Button(action: {
}) {
Text("Send")
.foregroundColor(.blue)
}
.buttonStyle(PlainButtonStyle())
.padding(.trailing, 20)
}
.padding(1)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
VStack {
HStack {
Spacer()
Button(action: {
}) {
Image(systemName: "gearshape")
.foregroundColor(.blue)
.padding()
}
.buttonStyle(PlainButtonStyle())
.padding(.trailing, 0)
.padding(.top, 0)
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
.padding(.trailing, 0)
.padding(.top, 20)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct VisualEffectView: NSViewRepresentable {
func makeNSView(context: Context) -> NSVisualEffectView {
let view = NSVisualEffectView()
view.blendingMode = .behindWindow
view.state = .active
view.material = .underWindowBackground
return view
}
func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
}
}
另外,作为奖励,是否可以使文本字段周围的区域/框更透明或带有一些模糊效果?就像第二个屏幕截图中显示的那样。
我尝试更改文本字段的背景颜色,例如.background(Color.blue)
,但整体外观不同。请参见下面的屏幕截图:
英文:
I was wondering if it's possible to change the color of the textfield inside the blue border itself, without changing the surrounding area that is semi-transparent. Here is a screenshot of what I have:
Here is a screenshot of what I wish to have:
Here is my code:
import SwiftUI
struct ContentView: View {
@State private var textFieldContent = ""
var body: some View {
ZStack {
VisualEffectView()
GeometryReader { geometry in
VStack {
Rectangle()
.foregroundColor(Color(red: 0.1, green: 0.1, blue: 0.1))
.frame(height: geometry.size.height / 1.16)
Rectangle()
.foregroundColor(.clear)
.frame(height: geometry.size.height * 2 / 3)
}
HStack {
TextField("Send a message", text: $textFieldContent)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.clear)
.cornerRadius(25)
.focusable(false)
.padding(.leading, 20)
Spacer()
Button(action: {
}) {
Text("Send")
.foregroundColor(.blue)
}
.buttonStyle(PlainButtonStyle())
.padding(.trailing, 20)
}
.padding(1)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
VStack {
HStack {
Spacer()
Button(action: {
}) {
Image(systemName: "gearshape")
.foregroundColor(.blue)
.padding()
}
.buttonStyle(PlainButtonStyle())
.padding(.trailing, 0)
.padding(.top, 0)
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
.padding(.trailing, 0)
.padding(.top, 20)
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct VisualEffectView: NSViewRepresentable {
func makeNSView(context: Context) -> NSVisualEffectView {
let view = NSVisualEffectView()
view.blendingMode = .behindWindow
view.state = .active
view.material = .underWindowBackground
return view
}
func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
}
}
Also, as a bonus, is it even possible to have the textfield surrounding the area/box more transparent or clear with some blur? As shown in the second screenshot.
I tried changing the textfield using .background(Color.blue)
for example, but the whole thing looks different then. See below:
答案1
得分: 1
以下是翻译好的部分:
这是如何完成的:
使用.textFieldStyle(.plain)
来擦除样式并自己构建文本框。
HStack {
TextField("发送消息", text: $textFieldContent)
.textFieldStyle(.plain)
.padding(6)
.padding(.horizontal, 6)
.background(Color.black.cornerRadius(6))
.padding()
.background(Color.clear)
.focusable(false)
Button(action: {
}) {
Text("发送")
.foregroundColor(.blue)
}
.buttonStyle(.plain)
}
.padding(.horizontal, 20)
.padding(1)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
使用这个解决方案,你将**没有焦点环,**但在你的目标示例中,它也是不可见的。
这不是你的问题,但我认为使用.padding(.horizontal, 20)
比在不同位置使用.leading
和.trailing
的两个填充更容易阅读。
英文:
Here's how you could do it:
use .textFieldStyle(.plain)
to erase styling and build the textbox yourself.
HStack {
TextField("Send a message", text: $textFieldContent)
.textFieldStyle(.plain) // 👈🏻
.padding(6)
.padding(.horizontal, 6)
.background(Color.black.cornerRadius(6))
.padding()
.background(Color.clear)
.focusable(false)
Button(action: {
}) {
Text("Send")
.foregroundColor(.blue)
}
.buttonStyle(.plain)
}
.padding(.horizontal, 20)
.padding(1)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
With this solution, you have no focus ring, but in your target example it's also not visible.
It was not your question, but I think it makes the code more readable to use .padding(.horizontal, 20)
rather than the two padding for .leading
and .trailing
in different places.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论