SwiftUI TabView: .tabItem with custom font does not work

4.3k Views Asked by At

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!

1

There are 1 best solutions below

2
On

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) }