Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set a custom font ot other stylings in Segmented Picker?

Tags:

swiftui

I want to set some font styling in a Picker with SegmentedPickerStyle(). But any font formatting is ignored. Is there a way to set the font for a segmentedPicker? I am pretty sure that it works for the default pickerWheel..

import UIKit
import PlaygroundSupport
import SwiftUI

 // Make a SwiftUI view
 struct ContentView: View {
  @State private var selection: Int = 2
     var body: some View {
      Picker("", selection: self.$selection) {
                    Text("Shape").tag(0)
                    Text("Height").tag(1)
        Text("Media").tag(2).font(.headline)
                    }
                      .font(.headline)
                      .cornerRadius(8)
                      .padding(10)
                      .pickerStyle(SegmentedPickerStyle())
     }
}

// Make a UIHostingController
let viewController = UIHostingController(rootView: ContentView())

// Assign it to the playground's liveView
PlaygroundPage.current.liveView = viewController

// RUN!

enter image description here

like image 785
Peter Pohlmann Avatar asked Sep 20 '19 17:09

Peter Pohlmann


1 Answers

SwiftUI uses UIKit (for iOS) under the hood, You should use appearance for UISegmentedControl:

struct ContentView: View {

    init() {
        UISegmentedControl.appearance().selectedSegmentTintColor = .red
        UISegmentedControl.appearance().setTitleTextAttributes(
            [
                .font: UIFont.boldSystemFont(ofSize: 24),
                .foregroundColor: UIColor.white
        ], for: .selected)

        UISegmentedControl.appearance().setTitleTextAttributes(
            [
                .font: UIFont.boldSystemFont(ofSize: 12),
                .foregroundColor: UIColor.blue
        ], for: .normal)
    }

    var body: some View { ,,, }
}
like image 156
Mojtaba Hosseini Avatar answered Oct 22 '22 11:10

Mojtaba Hosseini