From 178b0c568876daea58930275f10ab3be4e951bd0 Mon Sep 17 00:00:00 2001 From: timothybarraclough Date: Wed, 17 Aug 2016 16:10:28 +1200 Subject: [PATCH] add support for generating uiimages from css class codes (#89) --- FontAwesome/FontAwesome.swift | 36 +++++++++++++++++++++---- FontAwesomeTests/FontAwesomeTests.swift | 11 ++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/FontAwesome/FontAwesome.swift b/FontAwesome/FontAwesome.swift index fec3ef69..a13a05ab 100644 --- a/FontAwesome/FontAwesome.swift +++ b/FontAwesome/FontAwesome.swift @@ -50,7 +50,7 @@ public extension UIFont { /// A FontAwesome extension to String. public extension String { - + /// Get a FontAwesome icon string with the given icon name. /// /// - parameter name: The preferred icon name. @@ -58,23 +58,37 @@ public extension String { public static func fontAwesomeIconWithName(name: FontAwesome) -> String { return name.rawValue.substringToIndex(name.rawValue.startIndex.advancedBy(1)) } - + /// Get a FontAwesome icon string with the given CSS icon code. Icon code can be found here: http://fontawesome.io/icons/ /// /// - parameter code: The preferred icon name. /// - returns: A string that will appear as icon with FontAwesome. public static func fontAwesomeIconWithCode(code: String) -> String? { + + guard let name = self.fontAwesomeFromCode(code) else { + return nil + } + + return self.fontAwesomeIconWithName(name) + } + + /// Get a FontAwesome icon with the given CSS icon code. Icon code can be found here: http://fontawesome.io/icons/ + /// + /// - parameter code: The preferred icon name. + /// - returns: An internal corresponding FontAwesome code. + public static func fontAwesomeFromCode(code: String) -> FontAwesome? { + guard let raw = FontAwesomeIcons[code], icon = FontAwesome(rawValue: raw) else { return nil } - - return self.fontAwesomeIconWithName(icon) + + return icon } } /// A FontAwesome extension to UIImage. public extension UIImage { - + /// Get a FontAwesome image with the given icon name, text color, size and an optional background color. /// /// - parameter name: The preferred icon name. @@ -97,6 +111,18 @@ public extension UIImage { UIGraphicsEndImageContext() return image } + + /// Get a FontAwesome image with the given icon css code, text color, size and an optional background color. + /// + /// - parameter code: The preferred icon css code. + /// - parameter textColor: The text color. + /// - parameter size: The image size. + /// - parameter backgroundColor: The background color (optional). + /// - returns: A string that will appear as icon with FontAwesome + public static func fontAwesomeIconWithCode(code: String, textColor: UIColor, size: CGSize, backgroundColor: UIColor = UIColor.clearColor()) -> UIImage? { + guard let name = String.fontAwesomeFromCode(code) else { return nil } + return fontAwesomeIconWithName(name, textColor: textColor, size: size, backgroundColor: backgroundColor) + } } // MARK: - Private diff --git a/FontAwesomeTests/FontAwesomeTests.swift b/FontAwesomeTests/FontAwesomeTests.swift index 4060b7bd..2e042528 100644 --- a/FontAwesomeTests/FontAwesomeTests.swift +++ b/FontAwesomeTests/FontAwesomeTests.swift @@ -37,6 +37,11 @@ class FontAwesomeTests: XCTestCase { label.font = UIFont.fontAwesomeOfSize(200) label.text = String.fontAwesomeIconWithName(FontAwesome.Github) XCTAssertEqual(label.text, "\u{f09b}") + } + + func testLabelTextFromCode() { + let label = UILabel() + label.font = UIFont.fontAwesomeOfSize(200) label.text = String.fontAwesomeIconWithCode("fa-github") XCTAssertEqual(label.text, "\u{f09b}") } @@ -61,4 +66,10 @@ class FontAwesomeTests: XCTestCase { barItem.image = UIImage.fontAwesomeIconWithName(FontAwesome.Github, textColor: UIColor.blueColor(), size: CGSizeMake(4000, 4000), backgroundColor: UIColor.redColor()) XCTAssertNotNil(barItem.image) } + + func testIconImageFromCode() { + let barItem = UIBarButtonItem() + barItem.image = UIImage.fontAwesomeIconWithCode("fa-github", textColor: UIColor.blueColor(), size: CGSizeMake(4000, 4000), backgroundColor: UIColor.redColor()) + XCTAssertNotNil(barItem.image) + } }