• Stars
    star
    705
  • Rank 61,915 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 23 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command line utility to export colors, typography, icons and images from Figma to Xcode / Android Studio project

FigmaExport


SPM compatible GitHub license CocoaPods Compatible codebeat badge Test

Command line utility to export colors, typography, icons and images from Figma to Xcode / Android Studio project.

  • color - Figma's color style
  • typography - Figma's text style
  • icon — Figma's component with small black/colorized vector image
  • image — Figma's components with colorized image (Light/Dark)

The utility supports Dark Mode, SwiftUI and Jetpack Compose.

Why we've developed this utility:

  • Figma doesn't support exporting colors and images to Xcode / Android Studio. Manual export takes a long time.
  • For easy sync of the component library with the code

Articles:

Table of Contents:

Features

  • Export light & dark color palette directly to Xcode / Android studio project
  • Export icons to Xcode / Android Studio project
  • Export images to Xcode / Android Studio project
  • Export text styles to Xcode / Android Studio project
  • Supports Dark Mode
  • Supports High contrast colors for Xcode
  • Supports SwiftUI and UIKit
  • Supports Objective-C
  • Supports RTL

Exporting icons and images works only for Professional/Organisation Figma plan because FigmaExport use Shareable team libraries.

Result

iOS

Colors

When your execute figma-export colors command, figma-export exports colors from Figma directly to your Xcode project to the Assets.xcassets folder.

Figma light Figma dark Xcode

Additionally, the following Swift file will be created to use colors from the code.

 import UIKit
 
 extension UIColor {
    static var backgroundSecondaryError: UIColor { return UIColor(named: #function)! }
    static var backgroundSecondarySuccess: UIColor { return UIColor(named: #function)! }
    static var backgroundVideo: UIColor { return UIColor(named: #function)! }
    ...
 }

For SwiftUI the following Swift file will be created to use colors from the code.

 import SwiftUI
 
 extension Color {
    static var backgroundSecondaryError: Color { return Color(#function) }
    static var backgroundSecondarySuccess: Color { return Color(#function) }
    static var backgroundVideo: Color { return Color(#function) }
    ...
 }

If you set option useColorAssets: False in the configuration file, then will be generated code like this:

UIKit:

import UIKit

extension UIColor {
    static var primaryText: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor { traitCollection -> UIColor in
                if traitCollection.userInterfaceStyle == .dark {
                    return UIColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000)
                } else {
                    return UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)
                }
            }
        } else {
            return UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000)
        }
    }
    static var backgroundVideo: UIColor {
        return UIColor(red: 0.467, green: 0.012, blue: 1.000, alpha: 0.500)
    }
}

SwiftUI:

import SwiftUI

public extension ShapeStyle where Self == Color {
    static var primaryText: Color { Color(red: 1.000, green: 1.000, blue: 1.000, opacity: 1.000) }
}

Icons

Icons will be exported as PDF or SVG files with Template Image render mode.

Additionally, the following Swift file will be created to use icons from the code.

import UIKit

extension UIImage {
    static var ic16Notification: UIImage { return UIImage(named: #function)! }
    static var ic24ArrowRight: UIImage { return UIImage(named: #function)! }
    static var ic24Close: UIImage { return UIImage(named: #function)! }
    static var ic24Dots: UIImage { return UIImage(named: #function)! }
    ...
}

For SwiftUI the following Swift file will be created to use images from the code.

import SwiftUI

extension Image {
    static var ic16Notification: Image { return Image(#function) }
    static var ic24Close: Image { return Image(#function) }
    static var ic24DropdownDown: Image { return Image(#function) }
    static var ic24DropdownUp: Image { return Image(#function) }
    ...
}
...
VStack {
  Image.ic24Close
  Image.ic24DropdownDown
}
...

Images

Images will be exported as PNG files the same way.

Additionally, the following Swift file will be created to use images from the code.

import UIKit

extension UIImage {
    static var illZeroEmpty: UIImage { return UIImage(named: #function)! }
    static var illZeroNetworkError: UIImage { return UIImage(named: #function)! }
    static var illZeroServerError: UIImage { return UIImage(named: #function)! }
    ...
}

For SwiftUI a Swift file will be created to use images from the code.

Images with multiple idiom

If name of an image contains idiom at the end (e.g. ~ipad), it will be exported like this:

Typography

When your execute figma-export typography command figma-export generates 3 files:

  1. UIFont+extension.swift extension for UIFont that declares your custom fonts. Use these fonts like this UIFont.header(), UIFont.caption1().
  2. LabelStyle.swift struct for generating attributes for NSAttributesString with custom lineHeight and tracking (letter spacing).
  3. Label.swift file that contains base Label class and class for each text style. E.g. HeaderLabel, BodyLabel, Caption1Label. Specify these classes in xib files on in code.

Example of these files:

Android

Colors will be exported to values/colors.xml and values-night/colors.xml files. For Jetpack Compose, following code will be generated, if configured:

package com.redmadrobot.androidcomposeexample.ui.figmaexport

import ...

object Colors

@Composable
@ReadOnlyComposable
fun Colors.backgroundPrimary(): Color = colorResource(id = R.color.background_primary)

Icons will be exported to drawable directory as vector xml files. For Jetpack Compose, following code will be generated, if configured:

package com.redmadrobot.androidcomposeexample.ui.figmaexport

import ...

object Icons

@Composable
fun Icons.Ic24DropdownDown(
    contentDescription: String?,
    modifier: Modifier = Modifier,
    tint: Color = Color.Unspecified
) {
    Icon(
        painter = painterResource(id = R.drawable.ic_24_dropdown_down),
        contentDescription = contentDescription,
        modifier = modifier,
        tint = tint
    )
}

Vector images will be exported to drawable and drawable-night directories as vector xml files. Raster images will be exported to drawable-???dpi and drawable-night-???dpi directories as png or webp files.

Typography will be exported to values/typography.xml. For Jetpack Compose, following code will be generated, if configured:

package com.redmadrobot.androidcomposeexample.ui.figmaexport

import ...

object Typography {

    val body = TextStyle(
        fontFamily = FontFamily(Font(R.font.ptsans_regular)),
        fontSize = 16.0.sp,
        letterSpacing = 0.0.sp,
        lineHeight = 24.0.sp,
    )
}

Installation

Before installation you must provide Figma personal access token via environment variables.

export FIGMA_PERSONAL_TOKEN=value

This token gives you access to the Figma API. Generate a personal Access Token through your user profile page or on Figma API documentation website. If you use Fastlane just add the following line to fastlane/.env file

FIGMA_PERSONAL_TOKEN=value

Manual

Download the latest release and read Usage

Homebrew

brew install RedMadRobot/formulae/figma-export

If you want to export raster images in WebP format install cwebp command line utility.

brew install webp

CocoaPods + Fastlane

Add the following line to your Podfile:

pod 'FigmaExport'

This will download the FigmaExport binaries and dependencies in Pods/ during your next pod install execution and will allow you to invoke it via Pods/FigmaExport/Release/figma-export in your Fastfile.

Add the following line to your Fastfile:

lane :sync_colors do
  Dir.chdir("../") do
    sh "Pods/FigmaExport/Release/figma-export colors ."
  end
end

Don't forget to place figma-export.yaml file at the root of the project directory.

Run fastlane sync_colors to run FigmaExport.

Usage

  1. Open Terminal.app

  2. Go (cd) to the folder with figma-export binary file

  3. Run figma-export

    To export colors use colors argument:

    ./figma-export colors -i figma-export.yaml

    To export icons use icons argument:

    ./figma-export icons -i figma-export.yaml

    To export images use images argument:

    ./figma-export images -i figma-export.yaml

    To export typography use typography argument:

    ./figma-export typography -i figma-export.yaml

Android

In the figma-export.yaml file you must specify the following properties:

  • android.mainRes
  • android.resourcePackage if you want to generate Jetpack Compose code
  • android.mainSrc if you want to generate Jetpack Compose code
  • android.icons.output if you want to export icons
  • android.images.output if you want to export images

When you execute figma-export icons command, FigmaExport clears the {android.mainRes}/{android.icons.output} directory before exporting all the icons.

When you execute figma-export images command, FigmaExport clears the {android.mainRes}/{android.images.output} directory before exporting all the images.

Example folder structure:

main/
  res/
    figma-export-icons/
      drawable/
      drawable-night/
    figma-export-images/
      drawable/
      drawable-night/

Before first running figma-export you must add path to these directories in the app‘s build.gradle file.

...
android {
  ...
  sourceSets {
    main {
      res.srcDirs += "src/main/res/figma-export-icons"
      res.srcDirs += "src/main/res/figma-export-images"
    }
  }
}

Jetpack Compose

For Typography, Colors and Icons you can enable code generation for the use with Jetpack Compose in your config file:

  1. Configure android.mainSrc
  2. Configure android.[typography|colors|icons].composePackageName

Arguments

If you want to export specific colors/icons/images you can list their names in the last argument like this:

./figma-export icons "ic/24/edit" — Exports only one icon.

./figma-export icons "ic/24/edit, ic/16/notification" — Exports two icons

./figma-export icons "ic/24/videoplayer/*" — Exports all icons which names starts with ic/24/videoplayer/

./figma-export colors "common/*" — Exports all the colors which names starts with common

./figma-export colors — Exports all the colors.

⚠️ Wildcard doesn't work on Linux.

Argument -i or -input specifies path to FigmaExport configuration file figma-export.yaml.

Configuration

All available configuration options described in the CONFIG.md file.

Example of figma-export.yaml file for iOS project — Examples/Example/figma-export.yaml

Example of figma-export.yaml file for Android project — Examples/AndroidExample/figma-export.yaml

Generate figma-export.yaml config file using one of the following command:

figma-export init --platform android
figma-export init --platform ios

It will generate config file in the current directory.

Custom templates

FigmaExport uses Stencil and StencilSwiftKit to generate code.

iOS

If you want to modify structure of the generated *.swift files you should specify a directory (ios.templatesPath) where Stencil templates are located. If ios.templatesPath not specified default templates will be used.

Default Stencil templates for iOS located here: ./Sources/XcodeExport/Resources Custom Stencil templates for colors and images must have the following names:

  • UIColor+extension.swift.stencil for UIKit colors
  • Color+extension.swift.stencil for SwiftUI colors
  • UIImage+extension.swift.stencil for UIKit images
  • Image+extension.swift.stencil for SwiftUI images

Custom Stencil templates for typography must have the following names:

  • Label.swift.stencil,
  • LabelStyle.swift.stencil,
  • LabelStyle+extension.swift.stencil,
  • UIFont+extension.swift.stencil
  • Font+extension.swift.stencil.stencil
Android

If you want to modify structure of the generated .xml, .kt files you should specify a directory (android.templatesPath) where Stencil templates are located. If android.templatesPath not specified default templates will be used.

Defaul Stencil templates for Android located here: ./Sources/AndroidExport/Resources Custom Stencil templates must have the following names:

  • colors.xml.stencil
  • Colors.kt.stencil
  • Icons.kt.stencil
  • typography.xml.stencil
  • Typography.kt.stencil

Exporting Typography

iOS

  1. Add a custom font to the Xcode project. Drag & drop font file to the Xcode project, set target membership, and add font file name in the Info.plist file. See developer documentation for more info.
  2. Run figma-export typography to export text styles
  3. Use generated fonts and labels in your code. E.g. button.titleLabel?.font = UIFont.body(), let label = HeaderLabel().

Android

  1. Place font file under the res directory of your module
  2. Run figma-export typography to export text styles
  3. Create a top level style as a parent for the generated styles. For example:
<style name="FigmaExport.TextAppearance" parent="Widget.AppCompat">
</style>
  1. Use the generated styles in your code

Design requirements

If a color, icon or image is unique for iOS or Android platform, it should contains "ios" or "android" word in the description field in the properties. If a color, icon or image is used only by the designer, and it should not be exported, the word "none" should be specified in the description field.

If an icon supports RTL, it should contains "rtl" word in the description field in the properties.

Styles and Components must be published to a Team Library.

For figma-export colors

By default, if you support dark mode your Figma project must contains two files. One should contains a dark color palette, and the another light color palette. If you would like to specify light and dark colors in the same file, you can do so with the useSingleFile configuration option. You can then denote dark mode colors by adding a suffix like _dark. The suffix is also configurable. See CONFIG.md for more information in the colors section.

If you support high contrast mode without dark mode your Figma project must contains two files. One should contains a high contrast color palette, and the another light color palette. If you would like to specify light and high contrast colors in the same file, you can do so with the useSingleFile configuration option. You can then denote high contrast mode colors by adding a suffix like _lightHC. The suffix is also configurable. See CONFIG.md for more information in the colors section.

If you support high contrast mode with dark mode your Figma project must contains four files. Should be like this: light, dark, high contrast light, high contrast dark. If you would like to specify colors in the same file, you can do so with the useSingleFile configuration option. You can then denote high contrast mode colors by adding a suffix like _lightHC for light and _darkHC for dark high contrast colors. The suffix is also configurable. See CONFIG.md for more information in the colors section.

The light color palette may contain more colors than the dark or high light color palette wherein the dark color palette may contain more colors than the high dark color palette. If a light-only color is present, it will be considered as universal color for the iOS color palette. Names of the dark, high light and high dark colors must match the light colors.

Example

File Styles

For figma-export icons

By default, your Figma file should contains a frame with Icons name which contains components for each icon. You may change a frame name in a CONFIG.md file by setting common.icons.figmaFrameName property. If you support dark mode and want separate icons for dark mode, Figma project must contains two files. One should contains a dark icons, and another light icons. If you would like to have light and dark icons in the same file, you can do so with the useSingleFile configuration option. You can then denote dark mode icons by adding a suffix like _dark. The suffix is also configurable. See CONFIG.md for more information in the icons section.

For figma-export images

Your Figma file should contains a frame with Illustrations name which contains components for each illustration. You may change a frame name in a CONFIG.md file by setting common.images.figmaFrameName property.

If you support dark mode you must have two Figma files. The rules for these two files follow the same rules as described above for colors. But If you would like to specify light and dark illustrations in the same file, you can do so with the useSingleFile configuration option. You can then denote dark mode illustrations by adding a suffix like _dark. The suffix is also configurable. See CONFIG.md for more information in the illustrations section.

If you want to specify image variants for different devices (iPhone, iPad, Mac etc.), add an extra ~ mark with idiom name. For example add ~ipad postfix:

For figma-export typography.

Your Figma file must contains Text Styles.

Dynamic Type

It is recommended to support Dynamic Type. Dynamic Type provides additional flexibility by letting readers choose their preferred text size.

If you want to support Dynamic Type you should specify iOS native text style for your custom text styles in the description field of Text Style. Available iOS native text styles you can find on Human Interface Guidlines page in Typography/Dynamic Type Sizes.

For example: You have header text style with 20 pt font size. Native iOS text style that matches is "Title 3". In the description field of your header text style you should specify "Title 3".

Advice: Font in Tab Bar and standard Navigation Bar must not support Dynamic Type.

Example project

Example iOS projects, Android projects and example Figma files see in the Examples folder

Contributing

We'd love to accept your pull requests to this project.

License

figma-export is released under the MIT license. See LICENSE for details.

Feedback

If you have any issues with the FigmaExport or you want some new features feel free to create an issue, open a discussion or contact me.

Authors

Daniil Subbotin - [email protected]

More Repositories

1

input-mask-android

User input masking library repo.
Kotlin
1,210
star
2

input-mask-ios

User input masking library repo.
Swift
573
star
3

Chronos

Android library that handles asynchronous jobs.
Java
133
star
4

state-delegator

Collection of classes that helps you to manage a screen state.
Kotlin
132
star
5

kotlin-style-guide

red_mad_robot Kotlin Style Guide
87
star
6

PINkman

PINkman is a library to help implementing an authentication by a PIN code in a secure manner. The library derives hash from the user's PIN using Argon2 function and stores it in an encrypted file. The file is encrypted with the AES-256 algorithm in the GCM mode and keys are stored in the AndroidKeystore.
Kotlin
85
star
7

DAO

Swift
74
star
8

flipper

Flipper is a simple and useful tool to deal with feature toggles
Kotlin
70
star
9

gradle-version-catalogs

Version catalogs used in red_mad_robot Android team
Kotlin
68
star
10

RMRColorTools-iOS

Objective-C
67
star
11

catbird

Mock server for UI tests
Swift
50
star
12

edge-to-edge-decorator

Edge to edge decorator - is a utility class that is responsible for coloring the statusBar and navigationBar to maintain edge to edge (e2e) mode.
Kotlin
47
star
13

apexy-ios

The library for organizing a network layer in your awesome project.
Swift
44
star
14

NeumorphicWallet

💳 Neumorphic Wallet app concept in SwiftUI
Swift
43
star
15

PrioritizedTabBar

📱 Native iOS TabBar with prioritized layout
Swift
42
star
16

gradle-infrastructure

Set of small plugins to reduce boilerplate in Gradle build scripts.
Kotlin
42
star
17

omega-bank-ios

The Bank you really need.
Swift
28
star
18

redmadrobot-android-ktx

Missing Android KTX extensions.
Kotlin
27
star
19

figmiro-plugin

Figma Integration with Miro (Plugin)
TypeScript
26
star
20

Alarm-Clock

⏰ Alarm Clock / Proof of concept for our Colored Alarm Clock Interface shot @ Dribbble
Swift
26
star
21

perspective-animation-ios

Example of perspective photo animation from camera
Swift
24
star
22

mapmemory

Simple in-memory cache conception built on Map.
Kotlin
22
star
23

acronym-avatar

Library to show avatars with acronyms.
Kotlin
18
star
24

ACSlider

🎚 A slider displaying selected value
Swift
16
star
25

fastlane-plugin-jira_release_notes

Fastlane Plugin for Jira Release Notes
Ruby
15
star
26

synopsis

Swift source code scanner.
Swift
13
star
27

itemsadapter

The simple adapter to render static data in RecyclerView
Kotlin
13
star
28

golden-key

Security framework compatible with CryptoKit
Swift
12
star
29

android-mad-showcase

Welcome to Modern Android Development (MAD) Showcase, the Android application following modern practices: Kotlin, Coroutines, Flow, Compose, GraphQL etc
Kotlin
11
star
30

VolumeButtons

VolumeButtons provides interface for handle clicks on hardware volume buttons on iOS devices
Swift
10
star
31

navidux

Swift
10
star
32

DAO-generator

Swift
10
star
33

AndroidStudioTemplates

Templates for android studio
FreeMarker
9
star
34

android-docker-images

Docker images used in red_mad_robot Android team
Shell
7
star
35

fastlane-plugin-gmail

Fastlane Plugin for Gmail
Ruby
7
star
36

rmr_django

Template Django project
Python
7
star
37

http-transport

Synchronous HTTP transport library.
Swift
7
star
38

figmiro-server

This is proxy server for Figmiro plugin that works with Miro's REST API
TypeScript
6
star
39

FitnessAppWithDynamicIsland

Demo app designed to demostrate the capabilities of the new Apple Dynamic Island.
Swift
6
star
40

debug-panel-android

Library for easy application debugging
Kotlin
5
star
41

autograph

Swift source code generation kit.
Swift
5
star
42

frontend-robopractice-test-task

HTML
5
star
43

use-dropdown

HTML
4
star
44

FitnessDynamicIsland

Swift
4
star
45

service-autograph

Service generation utility.
Swift
4
star
46

fastlane-plugin-imessage

Ruby
4
star
47

model-compiler

Swift
4
star
48

danger-jira_issue_links

Danger plugin
Ruby
4
star
49

techradar-android

RMR Technology Radar
Kotlin
3
star
50

core-parser-generator

Swift
3
star
51

core-parser

Swift
3
star
52

parser-autograph

Object parser generation utility.
Swift
3
star
53

floating-text-input

Swift
2
star
54

opensource

Redmadrobot opensource
Ruby
2
star
55

homebrew-formulae

Redmadrobot Homebrew tap
Ruby
2
star
56

rmr-docker

Shell
1
star
57

service-generator

Swift
1
star
58

Toaster

Swift
1
star