有没有办法在 Segmented Picker 中设置其他样式的自定义字体?

Pet*_*ann 6 swiftui

我想使用 SegmentedPickerStyle() 在 Picker 中设置一些字体样式。但是任何字体格式都会被忽略。有没有办法为 segmentedPicker 设置字体?我很确定它适用于默认的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!
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Moj*_*ini 12

SwiftUI 在底层使用 UIKit(适用于 iOS),您应该将外观用于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 { ,,, }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是真的@MojtabaHosseini,因为它将更新所有 UISegmentedControls,因此在顶层仅设置一次是有效的。如果您只想更改单个分段选择器,那么这个答案是不够的 (2认同)
  • 当然比将其隐藏在子视图中更好。 (2认同)