【问题标题】:How do I style Text in SwiftUI Picker View?如何在 SwiftUI Picker View 中设置文本样式?
【发布时间】:2021-12-12 01:44:40
【问题描述】:

我在 SwiftUI 中有一个时间选择器,我对选择器中文本值的任何修改都将被忽略。我该如何正确地做到这一点?我在 Big Sur 上使用 Xcode 13。

这是未经任何修改的结果:

我希望时间更大更白,所以我添加了 .font().foregroundColor() 修饰符。这是我的代码:

struct TimerPicker: View {
    @Binding var customTime: CustomTime
    
    var body: some View {
        GeometryReader { geometry in
            HStack (spacing: 0.0){
                VStack {
                    Picker(selection: self.$customTime.selectedHour, label: Text("Hrs")) {
                        ForEach(0..<24) { hour in
                            Text("\(hour) hrs")
                                .foregroundColor(.white)
                                .font(.title3)
                        }
                    }
                }
                .frame(width: geometry.size.width * 0.33)
                .clipped()
                
                VStack {
                    Picker(selection: self.$customTime.selectedMin, label: Text("Min")) {
                        ForEach(0..<61) { min in
                            Text("\(min) min")
                                .foregroundColor(.white)
                                .font(.title3)
                        }
                    }
                }
                .frame(width: geometry.size.width * 0.33)
                .clipped()
                
                VStack {
                    Picker(selection: self.$customTime.selectedSecond, label: Text("Sec")) {
                        ForEach(0..<61) { sec in
                            Text("\(sec) sec")
                                .foregroundColor(.white)
                                .font(.title3)
                        }
                    }
                }
                .frame(width: geometry.size.width * 0.33)
                .clipped()
                .transition(.scale)
            }
        } //: Geometry
    }
}

选择器值的样式没有变化。这样做的正确方法是什么?

【问题讨论】:

    标签: swiftui uipickerview xcode13


    【解决方案1】:

    您的代码似乎对我有用,尽管我做了以下小的修改。 这是对我有用的代码: (注意没有 60 分钟或 60 秒)

    import SwiftUI
    
    @main
    struct TestApp: App {
        var body: some Scene {
            WindowGroup {
                ContentView()
            }
        }
    }
    
    struct ContentView: View {
        @State var customTime = CustomTime()
        
        var body: some View {
            Spacer()
            TimerPicker(customTime: $customTime)
            Spacer()
        }
    }
    
    struct CustomTime: Identifiable, Hashable {
        let id = UUID()
        var selectedHour = 0
        var selectedMin = 0
        var selectedSecond = 0
    }
    
    struct TimerPicker: View {
        @Binding var customTime: CustomTime
        
        var body: some View {
            GeometryReader { geometry in
                HStack (spacing: 0.0){
                    VStack {
                        Picker(selection: $customTime.selectedHour, label: Text("Hrs")) {
                            ForEach(0..<24) { hour in
                                Text("\(hour) hrs").tag(hour) // <--- tag
                                    .foregroundColor(.white)
                                    .font(.title3)
                            }
                        }
                    }
                    .frame(width: geometry.size.width * 0.33)
                    .clipped()
                    
                    VStack {
                        Picker(selection: $customTime.selectedMin, label: Text("Min")) {
                            ForEach(0..<60) { min in  // <--- only to 59
                                Text("\(min) min").tag(min) // <--- tag
                                    .foregroundColor(.white)
                                    .font(.title3)
                            }
                        }
                    }
                    .frame(width: geometry.size.width * 0.33)
                    .clipped()
                    
                    VStack {
                        Picker(selection: $customTime.selectedSecond, label: Text("Sec")) {
                            ForEach(0..<60) { sec in  // <--- only to 59
                                Text("\(sec) sec").tag(sec) // <--- tag
                                    .foregroundColor(.white)
                                    .font(.title3)
                            }
                        }
                    }
                    .frame(width: geometry.size.width * 0.33)
                    .clipped()
                    .transition(.scale)
                }
                .pickerStyle(.wheel)    // <--- here
                .background(Color.red)  // <--- here
            } //: Geometry
        }
    }
    

    【讨论】:

    • 这里的关键是添加 .pickerStyle(.wheel)。这恢复了文本的颜色。还有一个问题。当我尝试滚动时,每个滚轮的滚动都与左侧的滚动重叠。你也有这种情况吗?例如,如果我按秒滚动,它可以工作,但如果我尝试按分钟滚动,它会移动秒轮,而我根本无法移动小时轮。
    • 在这里查看我的答案:stackoverflow.com/questions/69365738/… 选择器相互重叠。如果它适合你,请打勾。
    • 我确实审查了它并且它确实有效。需要注意的是,您必须设置一个固定的 .frame 宽度才能使其正常工作。打勾。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2014-03-05
    • 2015-04-01
    • 1970-01-01
    相关资源
    最近更新 更多