All Projects → fernandodelrio → Swiftcolorgen

fernandodelrio / Swiftcolorgen

Licence: mit
A tool that generate code for Swift projects, designed to improve the maintainability of UIColors

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftcolorgen

Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (-1.32%)
Mutual labels:  cli, xcode, code-generation, code-generator
Swaggen
OpenAPI/Swagger 3.0 Parser and Swift code generator
Stars: ✭ 385 (+153.29%)
Mutual labels:  cli, generator, code-generation
Stevia
🍃 Concise Autolayout code
Stars: ✭ 3,182 (+1993.42%)
Mutual labels:  xcode, cocoapods, storyboard
Xcodegen
A Swift command line tool for generating your Xcode project
Stars: ✭ 5,032 (+3210.53%)
Mutual labels:  cli, xcode, generator
Localize
Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
Stars: ✭ 253 (+66.45%)
Mutual labels:  xcode, cocoapods, storyboard
Xcode One Dark
Atom One Dark theme for Xcode
Stars: ✭ 273 (+79.61%)
Mutual labels:  xcode, cocoapods, color
Uitextfield Navigation
🏄‍♂️ UITextField-Navigation makes it easier to navigate between UITextFields and UITextViews
Stars: ✭ 436 (+186.84%)
Mutual labels:  xcode, cocoapods, storyboard
Colorizeswift
Terminal string styling for Swift.
Stars: ✭ 253 (+66.45%)
Mutual labels:  xcode, cocoapods, color
Sidemenu
Simple side/slide menu control for iOS, no code necessary! Lots of customization. Add it to your project in 5 minutes or less.
Stars: ✭ 5,267 (+3365.13%)
Mutual labels:  xcode, cocoapods, storyboard
Cyltabbarcontroller
[EN]It is an iOS UI module library for adding animation to iOS tabbar items and icons with Lottie, and adding a bigger center UITabBar Item. [CN]【中国特色 TabBar】一行代码实现 Lottie 动画TabBar,支持中间带+号的TabBar样式,自带红点角标,支持动态刷新。【iOS13 & Dark Mode & iPhone XS MAX supported】
Stars: ✭ 6,605 (+4245.39%)
Mutual labels:  xcode, cocoapods, storyboard
Natrium
A pre-build (Swift) script to alter your Xcode project at pre-build-time per environment, build configuration and target.
Stars: ✭ 131 (-13.82%)
Mutual labels:  cli, xcode, cocoapods
Bartycrouch
Localization/I18n: Incrementally update/translate your Strings files from .swift, .h, .m(m), .storyboard or .xib files.
Stars: ✭ 1,032 (+578.95%)
Mutual labels:  xcode, code, storyboard
Mylinearlayout
MyLayout is a powerful iOS UI framework implemented by Objective-C. It integrates the functions with Android Layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,PathLayout,GridLayout,LayoutSizeClass to build your App 自动布局 UIView UITab…
Stars: ✭ 4,152 (+2631.58%)
Mutual labels:  xcode, cocoapods, storyboard
Laravel Code Generator
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.
Stars: ✭ 485 (+219.08%)
Mutual labels:  generator, code-generation, code-generator
Tangramkit
TangramKit is a powerful iOS UI framework implemented by Swift. It integrates the functions with Android layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICollectionView
Stars: ✭ 984 (+547.37%)
Mutual labels:  xcode, cocoapods, storyboard
Xcodecolorsense
🎈 An Xcode plugin that makes working with color easier
Stars: ✭ 79 (-48.03%)
Mutual labels:  xcode, rgb, color
Testdrive
Quickly try out any Swift pod or framework in a playground
Stars: ✭ 1,612 (+960.53%)
Mutual labels:  xcode, cocoapods
Gitignore It
📃 A CLI to generate .gitignore files
Stars: ✭ 132 (-13.16%)
Mutual labels:  cli, generator
Gk
Go-Kit Genetator
Stars: ✭ 136 (-10.53%)
Mutual labels:  cli, generator
Cloudkitgdpr
Framework for allowing users to manage data stored in iCloud
Stars: ✭ 126 (-17.11%)
Mutual labels:  xcode, cocoapods

SwiftColorGen

Swift 4.0 CocoaPods compatible License

A tool that generate code for Swift projects, designed to improve the maintainability of UIColors.

Please notice, this tool still under development. It's on a validation phase, where I'll test it integrated with existing iOS projects to see how useful it is. Feedbacks are appreciated. Also notice this tool not only generates new code, but also updates existing storyboard files, so keep your code under versioning control to avoid any data loss!

Table of contents

Motivation

Manage colors in iOS projects can be challenging. It would be useful to reuse colors in different places in the storyboard and also access them programmatically. In code, you can group the colors in one place, but it's common to have the same color redefined in many places in the storyboards. When you need to update a color, you need to remember to replace them everywhere and because of that, it becomes hard to maintain.

Since Xcode 9, we are able to define a color asset in the Assets catalog, allowing us to reuse a color inside the storyboards and access them programmatically. Though, this still isn't perfect:

  1. To access the colors programmatically, we use a string with the Asset name. If we rename the Asset, we need to remember to replace the strings referring the old asset
  2. If we rename an Asset, we also need to manually replace the references to them in the storyboards
  3. In an existing project with no color assets defined, we need to group all the colors in the storyboards, manually create the asset colors and replace them everywhere.

The solution

SwiftColorGen reads all storyboard files to find common colors, it creates them in a .xcassets folder (without any duplications) and refer them back in the storyboard. Then, it creates an UIColor extension allowing to access the same colors programmatically. It automatically puts a name to the colors found. The name will be the closest webcolor name, measuring the color distance between them. But, the user still can rename the colors and it will keep the storyboards updated.

The rules for naming the colors dinamically:

  • The closest web color name (https://en.wikipedia.org/wiki/Web_colors) is considered to name the color
  • If the alpha value is less than 255, an "alpha suffix" will be appended to the color name, to avoid name collision
  • If two RGB's are close to the same web color, the name still will be used if they have different alphas
  • If two RGB's are close to the same web color and they also have the same alpha, the hex of the RGB will be used to avoid name collision

SwiftColorGen is written in Swift and requires Swift to run. These are the dependencies:

Demo

That's the result of the code generation:

Collecting the colors on Storyboard and generating the Assets

Collecting Colors

Generating the Swift file

Swift File

Automatic renaming

Automatic Renaming

Custom colors + multiple replace

Custom Colors

Code generated

The tool should generate something like:

// Don't change. Auto generated file. SwiftColorGen
import UIKit

extension UIColor {
    /// Color #FFC034 (alpha 255)
    static var goldColor: UIColor {
        return UIColor(named: "Gold") ?? .clear
    }

    /// Color #8D00FF (alpha 255)
    static var darkVioletColor: UIColor {
        return UIColor(named: "DarkViolet") ?? .clear
    }

    /// Color #00FF00 (alpha 255)
    static var myCoolGreen: UIColor {
        return UIColor(named: "MyCoolGreen") ?? .clear
    }
}

You will probably want to rename the color and run the tool again, but you can just create another extension on another file referring the generated code:

extension UIColor {
    static var myTextColor: UIColor {
        return .darkVioletColor
    }
}

Here a complete video with the tool in action:

Demo

Using the CLI

First, install the dependencies:

$ swift package update

Build it:

$ swift build

Then run passing the appropriate parameters (if you prefer, you can also use the binary generated inside the .build folder):

Usage: swift run SwiftColorGen [options]
-o --outputFile (required):
    Path to the output Swift file
-b --baseFolder (optional):
    Path to the folder where the storyboards are located. Defaults to the current working directory
-a --assetsFolder (optional):
    Path to the assets folder. Defaults to the first .xcassets found under the base folder

Example:

$ swift run SwiftColorGen -o Example/Generated.swift

Notice that OS X 10.12 is required to run, because of a dependency from a NSColor method (used to convert between different color spaces)

Installation

You can install the tool using CocoaPods and then add a Build Phase step, that runs SwiftColorGen every time the project is built.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

  1. To integrate SwiftColorGen into your Xcode project, specify it in your Podfile:
pod 'SwiftColorGen'
  1. Install the dependencies:
$ pod install
  1. In Xcode: Click on your project in the file list, choose your target under TARGETS, click the Build Phases tab and add a New Run Script Phase by clicking the little plus icon in the top left. Drag the New Run Script Phase above the Compile Sources phase and below Check Pods Manifest.lock, expand it and then call the tool with something like that:
"$PODS_ROOT/SwiftColorGen/swiftcg" -b "$SRCROOT" -o "$SRCROOT/CustomColors.swift"
  1. Build your project and it's done. Remember to add the generated Swift file to Xcode, if it's not already there.

Contributing

This project still on a initial stage of development. Feel free to contribute by testing it and reporting bugs. If you want to help developing it, checkout the issues section. If you fixed some issue or made some enhancement, open a pull request.

License

SwiftColorGen is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].