0

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?

4
  • 1
    You'll need to swizzle preferredFont(forTextStyle:) ,because Menu and Picker with .pickerStyle(.menu) use it and ignore the .font() modifier. Commented Jul 5, 2024 at 9:38
  • It worked for the items of the picker, but not the selected one (blue text). I can see why this would be necessary (if it ignores the .font() modifier), but I'd rather find a solution without swizzling. Maybe I'll look for a Picker replacement? Commented Jul 6, 2024 at 12:27
  • Probably a safer choice. Either that, or hope Apple will fix it. Commented Jul 6, 2024 at 16:07
  • 1
    Since it was a nice challenge, I ended up implementing it: github.com/yonat/SystemFontOverride - feel free to use. Commented Jul 25, 2024 at 10:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.