From b9cc86a3fcd52e90c9c43154df58071cfd0ed541 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Thu, 19 Apr 2018 13:56:58 +0300 Subject: [PATCH 1/3] made FontLoader class public to make it possible to use SwiftIcons while creating an Attributed text of your own + added example into demo project --- Source/SwiftIcons.swift | 2 +- SwiftIcons/LibraryViewController.swift | 10 ++-- SwiftIcons/ObjectsDetailsViewController.swift | 54 ++++++++++++++++++- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 861ff59..86ab216 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -798,7 +798,7 @@ public extension UIViewController { } } -private class FontLoader { +public class FontLoader { /** This utility function helps loading the font if not loaded already diff --git a/SwiftIcons/LibraryViewController.swift b/SwiftIcons/LibraryViewController.swift index cc4975f..ab7b96b 100644 --- a/SwiftIcons/LibraryViewController.swift +++ b/SwiftIcons/LibraryViewController.swift @@ -26,11 +26,11 @@ 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), .fontAwesome(.pictureO), .fontAwesome(.tag), .ionicons(.iosCircleFilled), .fontAwesome(.pictureO), .ionicons(.iosStar), .ionicons(.iosToggle), .fontAwesome(.pictureO), .ionicons(.iphone), .fontAwesome(.ban), .fontAwesome(.ban)] + 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), .fontAwesome(.pictureO), .fontAwesome(.tag), .ionicons(.iosCircleFilled), .fontAwesome(.pictureO), .ionicons(.iosStar), .ionicons(.iosToggle), .fontAwesome(.pictureO), .ionicons(.iphone), .fontAwesome(.ban), .fontAwesome(.ban), .fontAwesome(.listOl)] override func viewDidLoad() { super.viewDidLoad() diff --git a/SwiftIcons/ObjectsDetailsViewController.swift b/SwiftIcons/ObjectsDetailsViewController.swift index f8a9cb1..68cc159 100644 --- a/SwiftIcons/ObjectsDetailsViewController.swift +++ b/SwiftIcons/ObjectsDetailsViewController.swift @@ -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() @@ -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 } @@ -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") } From 580e63f357e8ab78ec93405c737583c8d11f98a7 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Thu, 19 Apr 2018 14:05:06 +0300 Subject: [PATCH 2/3] loadFontIfNeeded and fontName methods also have to be public in order to use SwiftIcons while creating an Attributed text --- Source/SwiftIcons.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 86ab216..b781c6d 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -806,7 +806,7 @@ public class FontLoader { - Parameter fontType: The type of the font */ - static func loadFontIfNeeded(fontType : FontType) { + public static func loadFontIfNeeded(fontType : FontType) { let familyName = fontType.familyName() let fileName = fontType.fileName() @@ -897,7 +897,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(_): From ff57b18658f41c5b2e430f4bb7a9a80f79846af7 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Thu, 19 Apr 2018 14:16:09 +0300 Subject: [PATCH 3/3] added usage example in Readme --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 60eae80..a1a6826 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,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