diff --git a/Classes/FontAwesome.swift b/Classes/FontAwesome.swift index 5e3b942a..ca5f8c90 100644 --- a/Classes/FontAwesome.swift +++ b/Classes/FontAwesome.swift @@ -659,3 +659,23 @@ public extension String { return name.rawValue.substringToIndex(advance(name.rawValue.startIndex, 1)) } } + +public extension UIImage { + public static func fontAwesomeIconWithName(name: FontAwesome, textColor: UIColor, size: CGSize) -> UIImage { + let paragraph = NSMutableParagraphStyle() + paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping + paragraph.alignment = .Center + let attributedString = NSAttributedString(string: String.fontAwesomeIconWithName(name) as String, attributes: [NSFontAttributeName: UIFont.fontAwesomeOfSize(24.0), NSForegroundColorAttributeName: textColor, NSParagraphStyleAttributeName:paragraph]) + let size = sizeOfAttributeString(attributedString, size.width) + UIGraphicsBeginImageContextWithOptions(size, false , 0.0) + attributedString.drawInRect(CGRectMake(0, 0, size.width, size.height)) + let image = UIGraphicsGetImageFromCurrentImageContext() + UIGraphicsEndImageContext() + return image + } +} + +func sizeOfAttributeString(str: NSAttributedString, maxWidth: CGFloat) -> CGSize { + let size = str.boundingRectWithSize(CGSizeMake(maxWidth, 1000), options:(NSStringDrawingOptions.UsesLineFragmentOrigin), context:nil).size + return size +} diff --git a/Demo/FontAwesome.swift/Base.lproj/Main.storyboard b/Demo/FontAwesome.swift/Base.lproj/Main.storyboard index 25d039af..892e9771 100644 --- a/Demo/FontAwesome.swift/Base.lproj/Main.storyboard +++ b/Demo/FontAwesome.swift/Base.lproj/Main.storyboard @@ -1,7 +1,7 @@ - + - + @@ -34,6 +34,9 @@ + + + @@ -43,6 +46,7 @@ + diff --git a/Demo/FontAwesome.swift/ViewController.swift b/Demo/FontAwesome.swift/ViewController.swift index 4f452022..35a22046 100644 --- a/Demo/FontAwesome.swift/ViewController.swift +++ b/Demo/FontAwesome.swift/ViewController.swift @@ -28,6 +28,7 @@ class ViewController: UIViewController { @IBOutlet weak var label: UILabel! @IBOutlet weak var toolbarItem: UIBarButtonItem! @IBOutlet weak var button: UIButton! + @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() @@ -50,6 +51,9 @@ class ViewController: UIViewController { toolbarItem.setTitleTextAttributes(attributes, forState: .Normal) toolbarItem.title = String.fontAwesomeIconWithName(.Github) + // FontAwesome icon as image (for an UITabBarItem's image e. g.) + imageView.image = UIImage.fontAwesomeIconWithName(FontAwesome.Github, textColor: UIColor.blackColor(), size: CGSizeMake(30, 30)) + } } diff --git a/README.md b/README.md index 81bbb880..cf8d83f1 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,11 @@ toolbarItem.setTitleTextAttributes(attributes, forState: .Normal) toolbarItem.title = String.fontAwesomeIconWithName(.Github) ``` +### FontAwesome icon as a (tabbaritem's) image +```swift +tabBarItem.image = UIImage.fontAwesomeIconWithName(.Github, size: CGSizeMake(30, 30), textColor: UIColor.blackColor()) +``` + ## Requirements