Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds possibility to use icons from SwiftIcons while creating an arbitrary attributed text + restores example project + fixes issue #53 #31

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 8 additions & 13 deletions Source/SwiftIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)!

Expand Down Expand Up @@ -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(_):
Expand Down
2 changes: 1 addition & 1 deletion SwiftIcons.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down
8 changes: 4 additions & 4 deletions SwiftIcons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
Expand Down Expand Up @@ -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)";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
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), .fontAwesomeSolid(.listOl)]
override func viewDidLoad() {
super.viewDidLoad()

Expand Down
55 changes: 53 additions & 2 deletions SwiftIconsApp/ObjectsDetailsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down