5

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!

StingRay5
  • 2,388
  • 2
  • 20
  • 29

1 Answers1

14

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

Paulo Alb
  • 179
  • 1
  • 6
  • 1
    This is not correct. you are changing appearance globally. It may cause problem in other views. – Ali Pishvaee Nov 24 '21 at 12:40
  • It's a good compromise for apps that only have one tab bar until SwiftUI adds native support for custom font in `TabView`. – alobaili Mar 27 '23 at 10:38