Skip to content

Instantly share code, notes, and snippets.

@robbiet480
Created May 15, 2019 04:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbiet480/087c39461b5b8f146cad8e926ccc78b1 to your computer and use it in GitHub Desktop.
Save robbiet480/087c39461b5b8f146cad8e926ccc78b1 to your computer and use it in GitHub Desktop.
A SwiftGen template for generating icon enums and helpers.
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
import UIKit
{% if families %}
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% for family in families %}
{% set enumName %}{{param.enumName|default:family.name}}{% endset %}
/** A wrapper class for Objective-C compatibility. */
@objc {{accessModifier}} extension Iconic {
/** The icon font's family name. */
class var {{enumName|lowerFirstWord}}FamilyName: NSString {
return {{enumName}}.familyName as NSString
}
/** The icon font's total count of available icons. */
class var {{enumName|lowerFirstWord}}Count: Int {
return {{enumName}}.count
}
/**
Returns the icon font object for the specified size.
- parameter size: The size (in points) to which the font is scaled.
*/
class func {{enumName|lowerFirstWord}}Font(ofSize size: CGFloat) -> UIFont {
return {{enumName}}.font(ofSize: size)
}
/**
Returns the icon as an attributed string with the given pointSize and color.
- parameter icon: The icon type.
- parameter pointSize: The size of the font.
- parameter color: The tint color of the font.
*/
class func attributedString(withIcon icon: {{enumName}}, pointSize: CGFloat, color: UIColor?) -> NSAttributedString {
return icon.attributedString(ofSize: pointSize, color: color)
}
/**
Returns the icon as an attributed string with the given pointSize, color and padding.
- parameter icon: The icon type.
- parameter pointSize: The size of the font.
- parameter color: The tint color of the font.
- parameter edgeInsets: The edge insets to be used as horizontal and vertical padding.
*/
class func attributedString(withIcon icon: {{enumName}}, pointSize: CGFloat, color: UIColor?, edgeInsets: UIEdgeInsets) -> NSAttributedString {
return icon.attributedString(ofSize: pointSize, color: color, edgeInsets: edgeInsets)
}
/**
Returns the icon as an image with the given size and color.
- parameter icon: The icon type.
- parameter size: The size of the image, in points.
- parameter color: A tint color for the image.
*/
class func image(withIcon icon: {{enumName}}, size: CGSize, color: UIColor?) -> UIImage {
return icon.image(ofSize: size, color: color)
}
/**
Returns the icon as an image with the given size and color.
- parameter icon: The icon type.
- parameter size: The size of the image, in points.
- parameter color: The tint color of the image.
- parameter edgeInsets: The edge insets to be used as padding values.
*/
class func image(withIcon icon: {{enumName}}, size: CGSize, color: UIColor?, edgeInsets: UIEdgeInsets) -> UIImage {
return icon.image(ofSize: size, color: color, edgeInsets: edgeInsets)
}
/**
Registers the icon font with the font manager.
Note: an exception will be thrown if the resource (ttf/otf) font file is not found in the bundle.
*/
class func register{{enumName}}() {
{{enumName}}.register()
}
/**
Unregisters the icon font from the font manager.
*/
class func unregister{{enumName}}() {
{{enumName}}.unregister()
}
}
#if os(iOS) || os(tvOS)
/** An IconImageView subclass capable of rendering icons, compatible with Interface Builder. iOS and tvOS only. */
@IBDesignable
{{accessModifier}} final class {{enumName}}View: IconImageView {
/** The icon string name (Interface Builder only). */
@IBInspectable
@available(*, unavailable, message : "This attribute is restricted to be used in Interface Builder.")
dynamic {{accessModifier}} var iconName: String = "" {
willSet {
iconDrawable = {{enumName}}(named: newValue)
}
}
@available(*, unavailable, message : "This function is restricted to be used in Interface Builder.")
override {{accessModifier}} func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
// The icon font needs to be registered first.
{{enumName}}.register()
}
}
@objc {{accessModifier}} extension UIBarButtonItem {
/**
Initializes a new item using the specified icon and other properties.
- parameter icon: The icon to be used as image.
- parameter size: The size of the image, in points.
- parameter target: The object that receives the action message.
- parameter action: The action to send to target when this item is selected.
*/
convenience init(withIcon icon: {{enumName}}, size: CGSize, target: AnyObject?, action: Selector) {
let image = icon.image(ofSize: size, color: .black)
self.init(image: image, style: .plain, target: target, action: action)
}
}
@objc {{accessModifier}} extension UITabBarItem {
/**
Initializes a new item using the specified icon and other properties.
The tag is automatically assigned using the icon's raw integer value.
- parameter icon: The icon to be used as image.
- parameter size: The size of the image, in points.
- parameter title: The item's title. If nil, a title is not displayed.
*/
convenience init(withIcon icon: {{enumName}}, size: CGSize, title: String?) {
let image = icon.image(ofSize: size, color: .black)
self.init(title: title, image: image, tag: icon.rawValue)
}
}
@objc {{accessModifier}} extension UIButton {
/**
Sets the icon to use for the specified state.
- parameter icon: The icon to be used as image.
- parameter size: The size of the image, in points.
- parameter color: The color of the image.
- parameter state: The state that uses the specified title. The values are described in UIControl.State.
*/
func setIconImage(withIcon icon: {{enumName}}, size: CGSize, color: UIColor?, forState state: UIControl.State) {
let image = icon.image(ofSize: size, color: color ?? .black)
setImage(image, for: state)
}
}
#endif
/** A list with available icon glyphs from the icon font. */
@objc {{accessModifier}} enum {{enumName}}: Int, CaseIterable {
{% for icon in family.fonts[0].icons %}
case {{icon.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}Icon
{% endfor %}
/** The icon font's total count of available icons. */
{{accessModifier}} static var count: Int { return {{ family.fonts[0].icons.count }} }
}
extension {{enumName}} : IconDrawable {
/** The icon font's family name. */
{{accessModifier}} static var familyName: String {
return "{{family.name}}"
}
/**
Creates a new instance with the specified icon name.
If there is no valid name recognised, this initializer falls back to the first available icon.
- parameter iconName: The icon name to use for the new instance.
*/
{{accessModifier}} init(named iconName: String) {
switch iconName.lowercased() {
{% for icon in family.fonts[0].icons %}
case "{{icon.name|swiftIdentifier|lowercase}}": self = .{{icon.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}Icon
{% endfor %}
default: self = {{enumName}}(rawValue: 0)!
}
}
/**
Creates a new instance with the specified icon name.
If there is no valid name recognised, the given fallback value will be used.
If the fallback value is not recognised, this initializer falls back to the first available icon.
- parameter iconName: The icon name to use for the new instance.
- parameter fallbackIconName: The fallback icon name to use for the new instance if the iconName doesn't exist.
*/
{{accessModifier}} init(named iconName: String, fallbackIconName: String) {
switch iconName.lowercased() {
{% for icon in family.fonts[0].icons %}
case "{{icon.name|swiftIdentifier|lowercase}}": self = .{{icon.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}Icon
{% endfor %}
default: self = {{enumName}}(named: fallbackIconName)
}
}
/** The icon's name. */
{{accessModifier}} var name: String {
switch self {
{% for icon in family.fonts[0].icons %}
case .{{icon.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}Icon: return "{{icon.name|swiftIdentifier|lowercase}}"
{% endfor %}
}
}
/** The icon's unicode. */
{{accessModifier}} var unicode: String {
switch self {
{% for icon in family.fonts[0].icons %}
case .{{icon.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord}}Icon: return "{{icon.unicode|unicodeCase}}"
{% endfor %}
}
}
}
{% endfor %}
{% else %}
// No icons found for {{families[0]}} {{param.enumName}} font!
// Visit https://github.com/dzenbot/Iconic/issues for further information.
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment