SwiftUI TabView: .tabItem with custom font does not work
Asked Answered
R

1

8

Is anybody familiar with the issue that the tabItem of a SwiftUI TabView, doesn't apply custom fonts? At least not for tvOS13.

For the TabView itself a custom font is easily applied, but when trying to customise the font for the actual .tabItem, it doesn't do anything, but it also doesn't return any errors.

First I tried setting a let:

let fontCustom = Font.custom("Awesome Font Name", size: 25)

Then creating the TabView:

Text("Kanalen")
    .font(fontCustom)
...

Works, but then adding a TabItem to that view the same way doesn't:

...
.tabItem {
    HStack {
        Image(uiImage: UIImage(named: "icon.pdf")!)
        Text("Awesome Item")
            .font(Font.custom("Cera-Regular", size: 16))            
    }
}
...

Or like this:

.tabItem {
    HStack {
        Image(uiImage: UIImage(named: "icon.pdf")!)
        Text("Awesome Item")
            .font(Font.custom("Awesome Font Name", size: 16))            
    }
}

Anybody has a clue as to why this is not working, and does this mean I'll need to create a custom tabItem View completely?

Thanks all!

Remex answered 12/10, 2019 at 11:36 Comment(1)
I Have the same problem too.Alberic
N
16

This seems to do the trick, but uses the underlying UIKIt controls. Hope Apple implements this into swiftui soon.

init() { UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.init(name: "Avenir-Heavy", size: 15)! ], for: .normal) }

Naara answered 2/5, 2020 at 21:0 Comment(2)
This is not correct. you are changing appearance globally. It may cause problem in other views.Alberic
It's a good compromise for apps that only have one tab bar until SwiftUI adds native support for custom font in TabView.Christiniachristis

© 2022 - 2024 — McMap. All rights reserved.