RedMadRobot / figma-export

Command line utility to export colors, typography, icons and images from Figma to Xcode / Android Studio project
MIT License
719 stars 115 forks source link

Add support for Obj-C #93

Closed simonlee2 closed 3 years ago

simonlee2 commented 3 years ago

Description

Some older projects (like ours) may need to access the generated styles from objc, but the current codegen doesn't allow this. In order to achieve this, the @objc attribute can be optionally added in front of some generated properties.

Supported properties

Unsupported properties

I don't see a scenario where objc code would access properties generated for SwiftUI, so SwiftUI extension will ignore this option:

Additionally, the generated Labels use LabelStyle, which is defined as a struct. Adding support for objc would be a more significant and potentially breaking change:

Changes

Example output

Icons

public extension UIImage {
    @objc static var ic16KeyEmergency: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic16KeySandglass: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic16Notification: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24ArrowRight: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24Close: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24Dots: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24DropdownDown: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24DropdownUp: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24FullscreenDisable: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24FullscreenEnable: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24Profile: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var ic24ShareIos: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
}

Images

public extension UIImage {
    @objc static var imgZeroEmpty: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var imgZeroError: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var imgZeroInternet: UIImage { UIImage(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
}

Colors

public extension UIColor {
    @objc static var backgroundPrimary: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var backgroundSecondary: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var button: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var buttonPressed: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var textPrimary: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var textSecondary: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
    @objc static var tint: UIColor { UIColor(named: #function, in: BundleProvider.bundle, compatibleWith: nil)! }
}

Typography

public extension UIFont {

    @objc static func body() -> UIFont {
        customFont("PTSans-Regular", size: 16.0, textStyle: .body, scaled: true)
    }

    @objc static func caption() -> UIFont {
        customFont("PTSans-Regular", size: 14.0, textStyle: .footnote, scaled: true)
    }

    @objc static func header() -> UIFont {
        customFont("PTSans-Bold", size: 20.0)
    }

    @objc static func largeTitle() -> UIFont {
        customFont("PTSans-Bold", size: 34.0, textStyle: .largeTitle, scaled: true)
    }

    private static func customFont(
        _ name: String,
        size: CGFloat,
        textStyle: UIFont.TextStyle? = nil,
        scaled: Bool = false) -> UIFont {

        guard let font = UIFont(name: name, size: size) else {
            print("Warning: Font \(name) not found.")
            return UIFont.systemFont(ofSize: size, weight: .regular)
        }

        if scaled, let textStyle = textStyle {
            let metrics = UIFontMetrics(forTextStyle: textStyle)
            return metrics.scaledFont(for: font)
        } else {
            return font
        }
    }
}
subdan commented 3 years ago

Add to the Features section the following line - Supports Objective-C