diff --git a/README.md b/README.md index 4fb4c93..1316baf 100644 --- a/README.md +++ b/README.md @@ -373,6 +373,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 diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index c596a1d..74f7259 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -721,29 +721,24 @@ 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() 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!)! @@ -848,7 +843,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(_): 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..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)"; @@ -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"; diff --git a/SwiftIconsApp/LibraryViewController.swift b/SwiftIconsApp/LibraryViewController.swift index 44706ed..8b1e23d 100644 --- a/SwiftIconsApp/LibraryViewController.swift +++ b/SwiftIconsApp/LibraryViewController.swift @@ -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), .fontAwesomeSolid(.listOl)] + override func viewDidLoad() { super.viewDidLoad() diff --git a/SwiftIconsApp/ObjectsDetailsViewController.swift b/SwiftIconsApp/ObjectsDetailsViewController.swift index 21e8267..715137b 100644 --- a/SwiftIconsApp/ObjectsDetailsViewController.swift +++ b/SwiftIconsApp/ObjectsDetailsViewController.swift @@ -21,13 +21,14 @@ // SOFTWARE. import UIKit +import SwiftIcons 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() @@ -378,6 +379,53 @@ class ObjectsDetailsViewController: UIViewController { scrollView.addSubview(stepper1) + 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: [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 = .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: [NSAttributedString.Key.font: font, NSAttributedString.Key.foregroundColor: UIColor.green]) + } else { + bulletStr = NSAttributedString(string: "•", attributes: [:]) + } + + 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([NSAttributedString.Key.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 } @@ -553,6 +601,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") }