使用 SwiftUI 设置导航栏标题字体
Posted
技术标签:
【中文标题】使用 SwiftUI 设置导航栏标题字体【英文标题】:Set the Navigation Bar Title Font with SwiftUI 【发布时间】:2019-10-28 15:53:13 【问题描述】:这是一个 SwiftUI 问题,而不是 UIKit 问题。 :)
我正在尝试使用 SwiftUI 为导航栏标题设置不同的字体。我怀疑这还不被支持。这是我尝试过的:
var body: some View
NavigationView
.navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
无论我对.font
设置做什么,它都不会改变文本。我还尝试设置自定义字体并删除 displayMode
属性。
有没有人能够让它工作?
【问题讨论】:
【参考方案1】:在 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)
我希望这会对你有所帮助。谢谢!!
【讨论】:
【参考方案2】:如果你需要为 SF 系列使用新的圆角脸,你可以使用这个 sn-p
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]
【讨论】:
如何给 fontWeight = .bold ??有可能吗? @AnjaliKevadiya 可能是您可以使用func withFamily(_ newFamily: String) -> UIFontDescriptor
创建字体描述符,但您需要一个具体的圆角系统粗体字符串。我不知道。
@AnjaliKevadiya 在描述符上,可以添加 .withSymbolicTraits(UIFontDescriptor.SymbolicTraits.traitBold)!【参考方案3】:
您需要的所有设置都在 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)
P。 S:代码取自here
【讨论】:
以上是关于使用 SwiftUI 设置导航栏标题字体的主要内容,如果未能解决你的问题,请参考以下文章