【问题标题】:SwiftUI header fontSwiftUI 标题字体
【发布时间】:2021-07-08 20:50:23
【问题描述】:

我正在尝试找到一种获得List 标题的系统字体的好方法。这就是我的意思:

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("Test")) {
                Text("Blablabla")
            }
        }
    }
}

基本上,有没有办法以编程方式获取图像中显示“测试”的文本的字体,以便我可以在 Text 对象的其他地方使用它?

我是尝试使用DisclosureGroup 制作可折叠标题的字体,但是DisclosureGroup 的字体与Section 的字体不同。

【问题讨论】:

  • “获取字体”是指弄清楚是哪种字体,以便您可以在其他地方使用它吗?
  • @gavinmccabe 是的
  • 通常你可以使用 Debug View Hierarchy 进行检查,但它不适用于 SwiftUI...

标签: swift swiftui


【解决方案1】:

一次性使用:

Section(header: Text("Test").font(.title)) {
    Text("Blablabla")
}

多次使用:

首先,创建一个结构体:

struct CustomFont: View {
    var body: some View {
        Text("Test")
           .font(.title)
    }
 }

其次,调用struct的实例来使用它。

Section(header: CustomFont()) {
    Text("Blablabla")
}

【讨论】:

    【解决方案2】:

    另一种方法是提供你想要的字体,然后在你想用的任何地方使用它,很有趣:

    Text("Test").font(.largeTitle)
    
    Text("Test").font(.system(size:34))
    
    Text("Test").font(.system(size: 20, weight: .bold, design: .serif)))
    
    Text("Test").font(Font.custom("Wingdings", size: 30.0))
    
    Text("Test").font(Font.custom("Montserrat-Bold", size: 30.0))
    

    【讨论】:

      【解决方案3】:

      节标题的默认字体是.font(.headline)。并且文字是大写的。

      Text("Test".uppercased()).font(.headline)
      

      https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/typography/

      【讨论】:

        猜你喜欢
        • 2019-10-28
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2020-12-11
        • 1970-01-01
        • 2012-10-07
        相关资源
        最近更新 更多