Fonts 使用SwiftUI设置导航栏标题字体

Fonts 使用SwiftUI设置导航栏标题字体,fonts,navigationbar,swiftui,Fonts,Navigationbar,Swiftui,这是一个快速的问题,而不是UIKit问题。:) 我正在尝试使用SwiftUI为导航栏标题设置不同的字体。我怀疑这还没有得到支持。以下是我尝试过的: var body: some View { NavigationView { .navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large) } } 无论我如何使用.font设置,它都不会更改文本。我还尝试设置自定义字体并删除display

这是一个快速的问题,而不是UIKit问题。:)

我正在尝试使用SwiftUI为导航栏标题设置不同的字体。我怀疑这还没有得到支持。以下是我尝试过的:

var body: some View {
  NavigationView {
    .navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
  }
}
无论我如何使用
.font
设置,它都不会更改文本。我还尝试设置自定义字体并删除
displayMode
属性


有人能做到这一点吗?

在SwiftUI中,此时我们不能直接更改
navigationBarTitle
字体,但您可以像这样更改navigationBar的外观

struct YourView: View {
    init() {
        //Use this if NavigationBarTitle is with Large Font
        UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]

        //Use this if NavigationBarTitle is with displayMode = .inline
        //UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
    }
    var body: some View {
        NavigationView {
            Text("Hello World!")
              .navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
            //.navigationBarTitle (Text("Dashboard"), displayMode: .inline)
        }
    }
}

我希望这对你有帮助。谢谢

如果需要为SF族使用新的圆角面,可以使用此代码段

    let design = UIFontDescriptor.SystemDesign.rounded
    let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .largeTitle)
                                     .withDesign(design)!
    let font = UIFont.init(descriptor: descriptor, size: 48)
    UINavigationBar.appearance().largeTitleTextAttributes = [.font : font]

您需要的所有设置都在init()中。与他们一起玩,了解什么是他们的责任。几个小时前,这段代码帮助我理解了导航栏的设置。我不记得在哪里找到的

struct ContentView: View {
    init() {
        // this is not the same as manipulating the proxy directly
        let appearance = UINavigationBarAppearance()
        
        // this overrides everything you have set up earlier.
        appearance.configureWithTransparentBackground()
        
        // this only applies to big titles
        appearance.largeTitleTextAttributes = [
            .font : UIFont.systemFont(ofSize: 20),
            NSAttributedString.Key.foregroundColor : UIColor.white
        ]
        // this only applies to small titles
        appearance.titleTextAttributes = [
            .font : UIFont.systemFont(ofSize: 20),
            NSAttributedString.Key.foregroundColor : UIColor.white
        ]
        
        //In the following two lines you make sure that you apply the style for good
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
        UINavigationBar.appearance().standardAppearance = appearance
        
        // This property is not present on the UINavigationBarAppearance
        // object for some reason and you have to leave it til the end
        UINavigationBar.appearance().tintColor = .white
        
    }
    var body: some View {
        NavigationView {
            ZStack {
                Color.black
                    .edgesIgnoringSafeArea([.all])
                NavigationLink(destination: ContentView2()) {
                    Text("push")
                }
            }
            .navigationBarTitle("", displayMode: .inline)
            .navigationBarBackButtonHidden(true)
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
struct ContentView2: View {
    
    var body: some View {
        
            ZStack {
                Color.black
                    .edgesIgnoringSafeArea([.all])
                NavigationLink(destination: ContentView()) {
                    Text("push")
                }
            }
            .navigationBarTitle("My Custom White title", displayMode: .inline)
        
    }
}

注:代码取自

如何给出fontWeight=.bold??是否可能?@AnjaliKevadiya可能是您可以使用
func withFamily(uuNewFamily:String)->UIFontDescriptor
创建字体描述符,但您需要一个四舍五入系统粗体字体的具体字符串。我不知道。@AnjaliKevadiya在描述符上,你可以添加.withSymbolicTraits(UIFontDescriptor.SymbolicTraits.traitBold)!