swiftui - 有没有一种方法可以在“分段选择器”中将自定义字体设置为其他样式?

标签 swiftui

我想在带有PickeredPickerStyle()的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!

enter image description here

最佳答案

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 { ,,, }
}

关于swiftui - 有没有一种方法可以在“分段选择器”中将自定义字体设置为其他样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032826/

相关文章:

SwiftUI 生命周期应用程序 : How to change Status Bar Color

ios - 如何使用 SwiftUI 打开通知操作的自定义 View ?

Swiftui iOS 14 工具栏

variables - SwiftUI 在 Struct 之间共享变量

xcode - SwiftUI:选项卡更改后,选项卡 View 中的列表始终重置

ios - 将 SwiftUI 按钮绑定(bind)到 AnySubscriber,例如 RxCocoa 的按钮点击

ios - 如何在 SwiftUI 中更新文本标签?

ios - 即使不再显示,SwiftUI View 也会不断更新

swiftui - 仅在符号外的 Swift UI 着色

swiftui - 状态变量值正在丢失