Vaberer / Font-Awesome-Swift

Font Awesome swift library for iOS.
MIT License
743 stars 125 forks source link

UITabBarItem color options not working #69

Closed marcusleon closed 7 years ago

marcusleon commented 7 years ago

This works fine:

    let tabBarItem = self.tabBarController?.tabBar.items?.first
    tabBarItem?.setFAIcon(icon: .FABell)

The tab icon FABell icon appears.

But the image and text color is blue even though the defaults to the setFAIcon method are black.

Passing a value for the selected selectedTextColor and other color properties do not seem to have any affect on the color.

How can you change the color? Are we doing something incorrectly?

Thanks

marcusleon commented 7 years ago

Seems to be fixed by following the approach at http://stackoverflow.com/a/40319706/47281

Specifically adding .withRenderingMode(UIImageRenderingMode.alwaysOriginal) to the UIImage constructors and then adding the setTitleTextAttributes calls.

Updated FAIcon.swift:

public extension UITabBarItem {
    public func setFAIcon(icon: FAType, size: CGSize? = nil, textColor: UIColor = UIColor.black, backgroundColor: UIColor = UIColor.clear, selectedTextColor: UIColor = UIColor.black, selectedBackgroundColor: UIColor = UIColor.clear) {
        FontLoader.loadFontIfNeeded()
        let tabBarItemImageSize = size ?? CGSize(width: 30, height: 30)

        image = UIImage(icon: icon, size: tabBarItemImageSize, textColor: textColor, backgroundColor: backgroundColor).withRenderingMode(UIImageRenderingMode.alwaysOriginal)        
        selectedImage = UIImage(icon: icon, size: tabBarItemImageSize, textColor: selectedTextColor, backgroundColor: selectedBackgroundColor).withRenderingMode(UIImageRenderingMode.alwaysOriginal)

        setTitleTextAttributes([NSForegroundColorAttributeName: textColor], for: .normal)
        setTitleTextAttributes([NSForegroundColorAttributeName: selectedTextColor], for: .selected)     
    }
}

Is this a candidate change to be added to the lib?

Vaberer commented 7 years ago

thanks for your contribution