有没有办法使文本字段显示不同颜色,而不影响半透明的背景?

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月10日 11:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定