invalidArgument
invalidArgument

Reputation: 2465

Assign custom font to picker view choices

I use a custom font throughout my app (font is correctly setup). I'd like the SwiftUI Picker appearance to use the same custom font.

Here's a simpler sample code where the text should be in bold but is not. In my app, instead of .bold, I have a custom modifier that uses .font(.custom(Font.regular.name, size: 12.0, relativeTo: .body)) api. But as shown in the image, the text is not even bold.

Code:

enum PickerChoice: CaseIterable, Identifiable, Hashable {
    
    var id: Self { self }
    
    case choiceA
    case choiceB
    case iDontWantToChoose
    
    var label: String {
        switch self {
            case .choiceA:
                "Choice A"
            case .choiceB:
                "Choice B"
            case .iDontWantToChoose:
                "I don't want to choose"
        }
    }
}

struct ContentView: View {

    @State var pickerChoice: PickerChoice = .choiceA
    
    var body: some View {
        HStack(spacing: 0) {
            Text("Picker: ")
                .fontWeight(.black)
            
            Picker(selection: $pickerChoice) {
                ForEach(PickerChoice.allCases) {
                    Text($0.label)
                        .fontWeight(.black)
                        .tag($0.id)
                }
            } label: {
                Text("Picking...")
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding()
    }
}

#Preview {
    ContentView()
}

Current (unwanted) result: Screenshot of the sample app, showing the Picker with system font instead of bold font.

I want each picker row and the selected choice in the custom font. How to achieve this?

Upvotes: 0

Views: 106

Answers (0)

Related Questions