diff --git a/README.md b/README.md index 14b7ddb..55dbc78 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 255eea5..0c414a5 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -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() @@ -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(_): diff --git a/SwiftIconsApp/LibraryViewController.swift b/SwiftIconsApp/LibraryViewController.swift index 16e726d..cfefe66 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), .fontAwesome(.listOl)] + override func viewDidLoad() { super.viewDidLoad() diff --git a/SwiftIconsApp/ObjectsDetailsViewController.swift b/SwiftIconsApp/ObjectsDetailsViewController.swift index 26c84d2..fddc116 100644 --- a/SwiftIconsApp/ObjectsDetailsViewController.swift +++ b/SwiftIconsApp/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") }