Swiftui-我无法更改屏幕上的Picker文字的样式

发布于 2025-02-11 12:57:22 字数 1115 浏览 1 评论 0原文

我有一个这样的选择器:

Picker(languages[currentIndex].name,selection: $currentIndex){
                    ForEach(0..<languages.count, id: \.self){index in
                        Text(languages[index].name)
                            
                    }
                }

看起来像这样:

“在此处输入图像说明”

我正在尝试在屏幕截图上看到的蓝色'英语'文本。到目前为止,我试图对文本和拾音器本身进行一些样式,但我无法成功。

选择器本来是菜单样式的,因此我无法将拾音器更改为另一个(即车轮)。我明确提到了它,因为当我这样做时,它有效:

Picker(languages[currentIndex].name,selection: $currentIndex){
                    ForEach(0..<languages.count, id: \.self){index in
                        Text(languages[index].name)
                            .font(.custom("Montserrat Medium", size:16))
                    }
                }.pickerStyle(.wheel)

但是,这不是我想要的。我只需要为此蓝色的“英语”文本进行样式:

I have a picker like this:

Picker(languages[currentIndex].name,selection: $currentIndex){
                    ForEach(0..<languages.count, id: \.self){index in
                        Text(languages[index].name)
                            
                    }
                }

And it looks like this:

enter image description here

I'm trying to style the blue 'English' text that you see on the screenshot. So far I've tried to give some styling to the Text, and the Picker itself but I could not succeed it.

The picker is meant to be in menu style, so I can not change the pickerStyle to another one (i.e. wheel). I explicitly mentioned it because when I make it like this, it works:

Picker(languages[currentIndex].name,selection: $currentIndex){
                    ForEach(0..<languages.count, id: \.self){index in
                        Text(languages[index].name)
                            .font(.custom("Montserrat Medium", size:16))
                    }
                }.pickerStyle(.wheel)

However, this is not what I'm looking for. I only need to style this blue "English" text:

enter image description here

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2025-02-18 12:57:22

作为解决方法,您可以使用菜单而不是picker

struct ContentView: View {
    
    let languages = ["English", "German", "Spanish"]
    @State private var currentIndex = 0
    
    var body: some View {
        
        Menu {
            ForEach(0..<languages.count, id: \.self) { index in
                Button {
                    currentIndex = index
                } label: {
                    Text(languages[index])
                }
            }
        } label: {
            Text(languages[currentIndex])
                .foregroundColor(.orange)
                .bold()
        }
    }
}

As a workaround you can use Menu instead of Picker:

struct ContentView: View {
    
    let languages = ["English", "German", "Spanish"]
    @State private var currentIndex = 0
    
    var body: some View {
        
        Menu {
            ForEach(0..<languages.count, id: \.self) { index in
                Button {
                    currentIndex = index
                } label: {
                    Text(languages[index])
                }
            }
        } label: {
            Text(languages[currentIndex])
                .foregroundColor(.orange)
                .bold()
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文