Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Use a FontAwesome icon as a UITabBarItem image

I would like to use some icons in the FontAwesome Swift library as UITabBarItem images. I have been able to set the UITabBarItem title, but I am not able to set the UITabBarItem image. My code is as follows:

class AEVTabBarController: UITabBarController {

    var itemLabels = ["Promos", "Marcas", "Amigos", "Cerca de mí", "Más"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabItems = self.tabBar.items as [UITabBarItem]!

        for index in 0..<itemLabels.count {
            let currentItem = tabItems[index] as UITabBarItem
            currentItem.title = itemLabels[index]
            currentItem.image = String.fontAwesomeIconWithName(FontAwesome.Money) as UIImage
        }
    }
}

What the compiler is telling me is that 'String' is not convertible to 'UIImage'. Is there a way to convert a String into a UIImage or is there another way to solve this?

Thanks in advance.

like image 326
lmiguelvargasf Avatar asked Aug 31 '15 23:08

lmiguelvargasf


People also ask

How do I use Font Awesome regular icons?

You can place Font Awesome icons just about anywhere using the CSS Prefix fa and the icon's name. Font Awesome is designed to be used with inline elements (we like the <i> tag for brevity, but using a <span> is more semantically correct). icon If you change the font-size of the icon's container, the icon gets bigger.

How do I add Font Awesome icons to form?

The font-awesome icon can be placed by using the fa prefix before the icon's name. Example: In this example, we will take a form where the input field is necessary. After that, we will place the font-awesome icon inside the input field. We will use the CDN link to use the font-awesome icons.


1 Answers

Per the readme on that library you linked:

currentItem.image = UIImage.fontAwesomeIconWithName(.Money, , textColor: UIColor.blackColor(), size: CGSizeMake(30, 30))

The function you used returns a string that is the text shortcut for the icon. This would be useful for a label or button text that's using FontAwesome. What you're trying to do is set the image - I would re-read the Readme, and just get acquainted with the built in functions of that library.

like image 107
Chris Slowik Avatar answered Sep 17 '22 20:09

Chris Slowik