From b9cc86a3fcd52e90c9c43154df58071cfd0ed541 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Thu, 19 Apr 2018 13:56:58 +0300 Subject: [PATCH 1/7] 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/7] 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/7] 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 From 36e159e37047e9fea90df70c025a77714fb820e2 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Tue, 19 Feb 2019 18:19:39 +0200 Subject: [PATCH 4/7] deployment target set back to 9.3 --- SwiftIcons.podspec | 2 +- SwiftIcons.xcodeproj/project.pbxproj | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SwiftIcons.podspec b/SwiftIcons.podspec index afb72b4..ec089d5 100644 --- a/SwiftIcons.podspec +++ b/SwiftIcons.podspec @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.screenshots = 'https://raw.githubusercontent.com/ranesr/SwiftIcons/master/docs/images/pic01.png' s.source = { :git => 'https://github.com/ranesr/SwiftIcons.git', :tag => s.version } - s.ios.deployment_target = '10.3' + s.ios.deployment_target = '9.3' s.source_files = 'Source/SwiftIcons.swift' s.resource_bundle = { 'SwiftIcons' => 'Source/Fonts/*.ttf' } diff --git a/SwiftIcons.xcodeproj/project.pbxproj b/SwiftIcons.xcodeproj/project.pbxproj index 9e1fc4f..d3f07da 100644 --- a/SwiftIcons.xcodeproj/project.pbxproj +++ b/SwiftIcons.xcodeproj/project.pbxproj @@ -592,7 +592,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.3; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -643,7 +643,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 10.3; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; From e228a29f7991e7844c13ac6418d8d1a0454679d2 Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Tue, 19 Feb 2019 18:41:06 +0200 Subject: [PATCH 5/7] up to previous commit --- SwiftIcons.xcodeproj/project.pbxproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SwiftIcons.xcodeproj/project.pbxproj b/SwiftIcons.xcodeproj/project.pbxproj index d3f07da..e439ec0 100644 --- a/SwiftIcons.xcodeproj/project.pbxproj +++ b/SwiftIcons.xcodeproj/project.pbxproj @@ -502,7 +502,7 @@ GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 12.1; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.ranesr.SwiftIcons; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; @@ -532,7 +532,7 @@ GCC_C_LANGUAGE_STANDARD = gnu11; INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; - IPHONEOS_DEPLOYMENT_TARGET = 12.1; + IPHONEOS_DEPLOYMENT_TARGET = 9.3; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.ranesr.SwiftIcons; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; From e3fc8854939d72e7488a5c62923e19b4619f2f1f Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Tue, 19 Feb 2019 18:42:45 +0200 Subject: [PATCH 6/7] SwiftIconsApp example project should be working again --- SwiftIconsApp/LibraryViewController.swift | 2 +- SwiftIconsApp/ObjectsDetailsViewController.swift | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/SwiftIconsApp/LibraryViewController.swift b/SwiftIconsApp/LibraryViewController.swift index c5faba8..8b1e23d 100644 --- a/SwiftIconsApp/LibraryViewController.swift +++ b/SwiftIconsApp/LibraryViewController.swift @@ -31,7 +31,7 @@ class LibraryViewController: UICollectionViewController, UICollectionViewDelegat 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)] + 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), .fontAwesomeSolid(.listOl)] override func viewDidLoad() { super.viewDidLoad() diff --git a/SwiftIconsApp/ObjectsDetailsViewController.swift b/SwiftIconsApp/ObjectsDetailsViewController.swift index 3a63d57..715137b 100644 --- a/SwiftIconsApp/ObjectsDetailsViewController.swift +++ b/SwiftIconsApp/ObjectsDetailsViewController.swift @@ -21,6 +21,7 @@ // SOFTWARE. import UIKit +import SwiftIcons class ObjectsDetailsViewController: UIViewController { @@ -392,26 +393,26 @@ class ObjectsDetailsViewController: UIViewController { 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)!])] + let mySportsList: [NSAttributedString] = [NSAttributedString(string: "skiing", attributes: [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 15.0)!]), + NSAttributedString(string: "hiking", attributes: [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 15.0)!]), + NSAttributedString(string: "tennis", attributes: [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 15.0)!])] // preparing a bullet icon in a form of an NSAttributedString - let icon: FontType = .fontAwesome(.check) + let icon: FontType = .fontAwesomeSolid(.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]) + bulletStr = NSAttributedString(string: "\(iconText) ", attributes: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor.green]) } else { bulletStr = NSAttributedString(string: "•", attributes: [:]) } - let attributedString = NSMutableAttributedString(string: "Sports:\n", attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Heavy", size: 18.0)!]) + let attributedString = NSMutableAttributedString(string: "Sports:\n", attributes: [NSAttributedString.Key.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)) + bulletString.addAttributes([NSAttributedString.Key.paragraphStyle: createParagraphStyle()], range: NSMakeRange(0, bulletString.length)) attributedString.append(bulletString) } From dff34e32c0b776e280d9bd9544068e09379da2eb Mon Sep 17 00:00:00 2001 From: Vitaliy Tim Date: Tue, 19 Feb 2019 18:43:53 +0200 Subject: [PATCH 7/7] fix for issue #53: the app shouldn't crash when included as a static library --- Source/SwiftIcons.swift | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 256a6b3..74f7259 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -735,16 +735,10 @@ public class FontLoader { if !loadedFontsTracker[fontName]! { let bundle = Bundle(for: FontLoader.self) - var fontURL: URL! - let identifier = bundle.bundleIdentifier - - if (identifier?.hasPrefix("org.cocoapods"))! { - fontURL = bundle.url(forResource: fileName, withExtension: "ttf", subdirectory: "SwiftIcons.bundle") - } else { - fontURL = bundle.url(forResource: fileName, withExtension: "ttf")! - } - - let data = try! Data(contentsOf: fontURL) + let fontURL = bundle.url(forResource: fileName, withExtension: "ttf", subdirectory: "SwiftIcons.bundle") + ?? bundle.url(forResource: fileName, withExtension: "ttf") + guard let theFontURL = fontURL else { NSException(name: .internalInconsistencyException, reason: "failed to load font \(fontName)").raise(); return } + let data = try! Data(contentsOf: theFontURL) let provider = CGDataProvider(data: data as CFData) let font = CGFont(provider!)!