Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,21 @@ stepper.setIncrementIcon(icon: .ionicons(.iosPlay), forState: .normal)

```

### Using icons in your own attributed strings

```Swift

// Getting icon
let icon: FontType = .fontAwesome(.check)
FontLoader.loadFontIfNeeded(fontType: icon)

// Using it in your NSAttributedString
if let font = UIFont(name: icon.fontName(), size: 15), let iconText = icon.text {
let bulletStr = NSAttributedString(string: iconText, attributes: [NSAttributedStringKey.font: font])
}

```


## Examples

Expand Down
7 changes: 4 additions & 3 deletions Source/SwiftIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,15 @@ public extension UIViewController {
}
}

private class FontLoader {
public class FontLoader {

/**
This utility function helps loading the font if not loaded already

- Parameter fontType: The type of the font

*/
static func loadFontIfNeeded(fontType : FontType) {
public static func loadFontIfNeeded(fontType : FontType) {
let fileName = fontType.fileName()
let fontName = fontType.fontName()

Expand Down Expand Up @@ -844,7 +845,7 @@ public enum FontType: FontProtocol {
/**
This function returns the font name using font type
*/
func fontName() -> String {
public func fontName() -> String {
var fontName: String
switch self {
case .dripicon(_):
Expand Down
12 changes: 6 additions & 6 deletions SwiftIconsApp/LibraryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ private let reuseIdentifier = "cell"

class LibraryViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

var topBackgroundColors = ["e74c3c", "e67e22", "f1c40f", "2ecc71", "1abc9c", "3498db", "9b59b6", "E4ACCF", "95a5a6", "34495e", "6c6998"]
var bottomBackgroundColors = ["c0392b", "d35400", "f39c12", "27ae60", "16a085", "2980b9", "8e44ad", "B68AA5", "7f8c8d", "2c3e50", "8781bd"]
var objects = ["UIImage", "UIImageView", "UILabel", "UIButton", "UISegmentedControl", "UITabBarItem", "UISlider", "UIBarButtonItem", "UIViewController", "UITextField", "UIStepper"]
var objectNames = ["Image", "Image View", "Label", "Button", "Segmented Control", "Tab Bar Item", "Slider", "Item", "View Controller", "Text", "Stepper"]
var icons: [FontType] = [.ionicons(.images), .fontAwesomeRegular(.building), .fontAwesomeRegular(.grinWink), .ionicons(.iosCircleFilled), .fontAwesomeRegular(.handRock), .ionicons(.iosStar), .ionicons(.iosToggle), .fontAwesomeRegular(.kissBeam), .ionicons(.iphone), .fontAwesomeRegular(.flushed), .fontAwesomeRegular(.smile)]

var topBackgroundColors = ["e74c3c", "e67e22", "f1c40f", "2ecc71", "1abc9c", "3498db", "9b59b6", "E4ACCF", "95a5a6", "34495e", "6c6998", "46C015"]
var bottomBackgroundColors = ["c0392b", "d35400", "f39c12", "27ae60", "16a085", "2980b9", "8e44ad", "B68AA5", "7f8c8d", "2c3e50", "8781bd", "46C0C0"]
var objects = ["UIImage", "UIImageView", "UILabel", "UIButton", "UISegmentedControl", "UITabBarItem", "UISlider", "UIBarButtonItem", "UIViewController", "UITextField", "UIStepper", "NSAttributedString"]
var objectNames = ["Image", "Image View", "Label", "Button", "Segmented Control", "Tab Bar Item", "Slider", "Item", "View Controller", "Text", "Stepper", "Attributed String"]
var icons: [FontType] = [.ionicons(.images), .fontAwesomeRegular(.building), .fontAwesomeRegular(.grinWink), .ionicons(.iosCircleFilled), .fontAwesomeRegular(.handRock), .ionicons(.iosStar), .ionicons(.iosToggle), .fontAwesomeRegular(.kissBeam), .ionicons(.iphone), .fontAwesomeRegular(.flushed), .fontAwesomeRegular(.smile), .fontAwesome(.listOl)]
override func viewDidLoad() {
super.viewDidLoad()

Expand Down
54 changes: 52 additions & 2 deletions SwiftIconsApp/ObjectsDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ObjectsDetailsViewController: UIViewController {

@IBOutlet var scrollView: UIScrollView!
var index: Int!
var textColors = ["e74c3c", "e67e22", "f1c40f", "2ecc71", "1abc9c", "3498db", "9b59b6", "e4Accf", "95a5a6", "34495e", "6c6998"]
var objects = ["UIImage", "UIImageView", "UILabel", "UIButton", "UISegmentedControl", "UITabBarItem", "UISlider", "UIBarButtonItem", "UIViewController", "UITextField", "UIStepper"]
var textColors = ["e74c3c", "e67e22", "f1c40f", "2ecc71", "1abc9c", "3498db", "9b59b6", "e4Accf", "95a5a6", "34495e", "6c6998", "6c6998"]
var objects = ["UIImage", "UIImageView", "UILabel", "UIButton", "UISegmentedControl", "UITabBarItem", "UISlider", "UIBarButtonItem", "UIViewController", "UITextField", "UIStepper", "NSAttributedString"]

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -389,6 +389,53 @@ class ObjectsDetailsViewController: UIViewController {
scrollView.addSubview(stepper1)
break

case 11:

func createParagraphStyle() -> NSParagraphStyle {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 15, options: [:])]
paragraphStyle.defaultTabInterval = 15
paragraphStyle.firstLineHeadIndent = 0
paragraphStyle.headIndent = 15
return paragraphStyle
}

let label = UILabel(frame: CGRect(x: 20, y: 20, width: screenWidth/2-40, height: screenWidth/2-40))
label.numberOfLines = 0

let mySportsList: [NSAttributedString] = [NSAttributedString(string: "skiing", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Heavy", size: 15.0)!]),
NSAttributedString(string: "hiking", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Heavy", size: 15.0)!]),
NSAttributedString(string: "tennis", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Heavy", size: 15.0)!])]

// preparing a bullet icon in a form of an NSAttributedString
let icon: FontType = .fontAwesome(.check)
FontLoader.loadFontIfNeeded(fontType: icon)
let bulletStr: NSAttributedString
if let font = UIFont(name: icon.fontName(), size: 15), let iconText = icon.text {
bulletStr = NSAttributedString(string: "\(iconText) ", attributes: [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: UIColor.green])
} else {
bulletStr = NSAttributedString(string: "•", attributes: [:])
}

let attributedString = NSMutableAttributedString(string: "Sports:\n", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Heavy", size: 18.0)!])
for string in mySportsList {
let bulletString = NSMutableAttributedString(attributedString: bulletStr)
bulletString.append(string)
bulletString.append(NSAttributedString(string: "\n"))
bulletString.addAttributes([NSAttributedStringKey.paragraphStyle: createParagraphStyle()], range: NSMakeRange(0, bulletString.length))
attributedString.append(bulletString)
}

label.attributedText = attributedString
label.isUserInteractionEnabled = true
label.tag = 40
let tap40 = UITapGestureRecognizer(target: self, action: #selector(tapped(gesture:)))
label.addGestureRecognizer(tap40)

scrollView.addSubview(label)
scrollView.contentSize = CGSize(width: screenWidth, height: 3*screenWidth/2-40)
break

default:
break
}
Expand Down Expand Up @@ -564,6 +611,9 @@ class ObjectsDetailsViewController: UIViewController {
case 39:
print("button.setIcon(icon: .mapicons(.airport), iconColor: textColor, title: \"AIRPLANE\", font: font!, forState: .normal)")

case 40:
print("let icon: FontType = .fontAwesome(.check)\n FontLoader.loadFontIfNeeded(fontType: icon)\n let myIconAsAttributedString = NSAttributedString(string: icon.text!, attributes: [NSAttributedStringKey.font: UIFont(name: icon.fontName(), size: 15)!]) }")

default:
print("Default")
}
Expand Down