Skip to content

Commit

Permalink
add support for generating uiimages from css class codes (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothybarraclough authored and thii committed Aug 17, 2016
1 parent e6fd79f commit 178b0c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
36 changes: 31 additions & 5 deletions FontAwesome/FontAwesome.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,45 @@ 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.
/// - returns: A string that will appear as icon with FontAwesome.
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.
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions FontAwesomeTests/FontAwesomeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
Expand All @@ -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)
}
}

0 comments on commit 178b0c5

Please sign in to comment.