我正在将SwiftUI与Xcode 11结合使用,我想用以下代码行更改NavigationBarTitle
字体:
.navigationBarTitle (Text("Navigation Bar Title"), displayMode: .inline)
.font(.subheadline)
但是什么也没发生。有任何建议或评论吗?
答案 0 :(得分:6)
在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("TEST")
.navigationBarTitle (Text("TEST"), displayMode: .inline)
}
}
}
我希望它将对您有所帮助。谢谢!
答案 1 :(得分:0)
您不能使用navigationBarTitle
修饰符还更改.font
的字体(自Xcode 11-Beta2起经过测试)。
但是您可以构建自己的自定义navigationBarView
,并可以完全访问其任何子视图,如下所示:
struct NavigationBarView: View {
var body: some View {
VStack(spacing: 0) {
Rectangle()
.foregroundColor(Style.Color.navigationBarColor)
.edgesIgnoringSafeArea(.top)
.frame(height: 0)
Rectangle()
.foregroundColor(Style.Color.navigationBarColor)
.frame(height: 64)
.overlay(
HStack() {
Image("close")
Spacer()
Text("سفر به سلامت")
.font(Style.Font.navigationTitle)
.color(Style.Color.darkTextColor)
.multilineTextAlignment(.center)
Spacer()
Image("menu")
}
)
Spacer()
}
.edgesIgnoringSafeArea(.horizontal)
}
}
请注意,您将利用此实现失去LargeTitleStyle
。
答案 2 :(得分:0)
我认为您必须创建一个NavigationBarBuilder。
<ng-template #resourceHeaderTemplate let-data>
<!-- here I have the professional id -->
<div class='template-wrap'>
<div class="avatar resource-image {{getDoctorImage(data)}}"></div>
<div class="resource-details">
<div class="resource-name">{{getDoctorName(data)}}</div>
<div class="h6">{{ getEspecialidadeName(data) }}</div>
<div class="h5 text-bold" [ngClass]="{'green-fg': getDoctorStatus(data).type === 'atendendo',
'secondary-text': getDoctorStatus(data).type === 'away' }">{{ getDoctorStatus(data).text }}</div>
</div>
</div>
</ng-template>
<ng-template #dateHeaderTemplate let-data>
<div fxLayout="column" fxLayoutAlign="center center">
<div class="date-text h3 text-bold">{{getDateHeaderText(data.date)}}</div>
<div class="h6">{{getStartTimeAndEndTime(data)}}</div>
<!--
Here besides getting the date I also need the professional id
that comes inside the ng-template #resourceHeaderTemplate-->
</div>
</ng-template>
<e-resources>
<e-resource field='DoctorId' title='Doctor' name='Doctors' [dataSource]='resourceDataSource'
textField='text' idField='id' colorField='color' workDaysField='workDays' startHourField='startHour'
endHourField='endHour'>
</e-resource>
</e-resources>
,您可以在内容视图中使用它。
struct NavigationBarBuilder: UIViewControllerRepresentable {
var build: (UINavigationController) -> Void = { _ in }
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationBarBuilder>) -> UIViewController {
UIViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationBarBuilder>) {
if let navigationController = uiViewController.navigationController{
self.build(navigationController)
}
}
}
祝你好运...
答案 3 :(得分:0)
在SWIFT 5中,UINavigationBar.appearance()。titleTextAttributes不起作用。您需要使用以下方法之一来更改标题中的字体:
init () {
UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia", size: 20)!]
// OR
UINavigationBar.appearance().largeTitleTextAttributes = [.font:UIFont.preferredFont(forTextStyle:.text-style)]
}
.text样式可在UIFont.TextStyle文档中找到,例如
.caption
.body
.title1
.title2
.title3
etc