Search code examples
iosswiftxcodexcode11

SwiftUI Not Applying Custom Font to Tabbed View


I'm trying to display FontAwesome Icons in my tabbed view but swift isn't having it.

Here is my code:

        TabView(selection: $selection){
            Text(" \u{f466} First View")
                .tabItem {


                          Text("\u{f466} First Vw")

                }
                .tag(0)
            Text("Second View")
                .tabItem {
                       Text("\u{f466} First View")

                }
                .tag(1)
        }.font(Font.custom("Font Awesome 5 Free", size:18))

It's utilizing the custom font in the first Text (main labels) but not in the tabs.

How do I fix this?

Thanks![enter image description here]1


Solution

  • FontAwesome unfortunately does not work with tabItem. As for the reason why, so far I couldn't find it. As you can see, in simple Text, it works!

    The good news is, there's a work around for that.

    Using FontAwesome.swift

    pod 'FontAwesome.swift'
    

    Github: https://github.com/thii/FontAwesome.swift

    var body: some View {
            TabView(selection: $selection){
                Text("First View")
                    .tabItem {
                        Image(uiImage: UIImage.fontAwesomeIcon(name: .coffee, style: .solid, textColor: .black, size: CGSize(width: 30, height: 30)))
                        Text("First Vw")
                }
                .tag(0)
                Text("\u{f075} Second View")
                    .font(Font.custom("FontAwesome5Free-Solid", size:18))
                    .tabItem {
                        Text("\u{f075} First View")
                }
                .tag(1)
            }.font(Font.custom("FontAwesome5Free-Solid", size:18))
        }
    

    We make an Image object for our tabItem, but using UIImage. And that UIImage will be coming from FontAwesome.

    enter image description here