Skip to content

Commit

Permalink
Added an UIImage extension to use FontAwesome as image
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kautz committed Jun 3, 2015
1 parent d50c2f2 commit 7ba3a83
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
20 changes: 20 additions & 0 deletions Classes/FontAwesome.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions Demo/FontAwesome.swift/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6751" systemVersion="14D105g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="aRy-2i-aVc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E26a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="aRy-2i-aVc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down Expand Up @@ -34,6 +34,9 @@
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="JKa-MA-t89">
<rect key="frame" x="172" y="319" width="30" height="30"/>
</imageView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
Expand All @@ -43,6 +46,7 @@
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
<connections>
<outlet property="button" destination="GnA-qd-hzW" id="bOb-BJ-gky"/>
<outlet property="imageView" destination="JKa-MA-t89" id="uZo-RB-NSc"/>
<outlet property="label" destination="QJK-GN-cIo" id="PIi-HE-hTp"/>
<outlet property="leftBarButton" destination="GiA-rH-r6O" id="Wle-vD-13q"/>
<outlet property="toolbarItem" destination="ZcV-cv-6rW" id="8XP-V5-0gH"/>
Expand Down
4 changes: 4 additions & 0 deletions Demo/FontAwesome.swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))

}
}

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7ba3a83

Please sign in to comment.