|
1 |
| -#objc-assetgen |
| 1 | +#codegenutils |
| 2 | + |
| 3 | +We love Xcode 5. It's been adding way cool features at a really fast pace, and adopting these features is the quickest way to bliss in life. |
| 4 | + |
| 5 | +(Or at least iOS development bliss.) |
| 6 | + |
| 7 | +But sometimes these boss new features don't quite get all the way. In the interest of meeting the tools halfway, we present three small code generators to improve your life in subtle ways. |
| 8 | + |
| 9 | +##objc-assetgen |
2 | 10 |
|
3 | 11 | Xcode 5 has an awesome new feature called "[asset catalogs](https://developer.apple.com/technologies/tools/features.html)", allowing you to specify all of your image variants and resizable cap insets in a single place.
|
4 | 12 |
|
5 | 13 | Unfortunately, to get the full benefits, you have to set your deployment target to iOS 7; otherwise, Xcode will dutifully put all of the images into your app bundle but totally ignore all of your resizable insets with only a build warning.
|
6 | 14 |
|
| 15 | +And even if you're targeting iOS 7, you'll still have to deal with typing string filenames correctly. And[everybody knows that stringly-typed code is bad.](http://c2.com/cgi/wiki?StringlyTyped) |
| 16 | + |
7 | 17 | But shucks! The important and awesome part is the editor, and it puts all of the data out in super-readable JSON. We should be able to do a poor man's version that reads in the data and spits out some code to give you most of the benefits.
|
8 | 18 |
|
9 |
| -##Usage |
| 19 | +###Usage |
10 | 20 |
|
11 | 21 | Call`objc-assetgen` with the`.xcassets` paths as arguments from the directory into which it should output the code.
|
12 | 22 |
|
13 |
| -For an asset catalog named "Foo" containing image sets "Bar" and "Baz", you'll get`FooCatalog.h` and`FooCatalog.m`, with class methods`+ (UIImage *)imageForBar` and`+ (UIImage *)imageForBaz`. Put them in your DerivedSources folder and you're good to go. |
| 23 | +For an asset catalog named "Foo" containing image sets "Bar" and "Baz", you'll get`FooCatalog.h` and`FooCatalog.m`, with class methods`+ (UIImage *)barImage` and`+ (UIImage *)bazImage`. Put them in your DerivedSources folder and you're good to go. |
| 24 | + |
| 25 | +##objc-colordump |
| 26 | + |
| 27 | +On Mac OS X, the standard system color picker has a tab for palette-based colors: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +One little-known feature of this color picker tab is that you can create your own palettes and fill them with the custom colors for your app. |
| 32 | + |
| 33 | +In the spirit of[Once and Only Once](http://c2.com/cgi/wiki?OnceAndOnlyOnce), once you've used these colors in your storyboards and nib files, you won't want to have to define them again for that rare occasion you're using them in code. Luckily, your custom color palettes are just files in`~/Library/Colors`, and we can read them and give your app access to them. |
| 34 | + |
| 35 | +Running the tool will also install the palette into your system color picker, so the other developers on your team can use your colors in their storyboards. |
| 36 | + |
| 37 | +###Usage |
14 | 38 |
|
15 |
| -##Command-line options |
| 39 | +Call`objc-colordump` with the`.clr` paths as arguments from the directory into which it should output the code. |
| 40 | + |
| 41 | +For a color palette named "Foo" colors named "Bar" and "Baz", you'll get`FooColorList.h` and`FooColorList.m`, with class methods`+ (UIColor *)barColor` and`+ (UIColor *)bazColor`. Put them in your DerivedSources folder and you're good to go. |
| 42 | + |
| 43 | +##objc-identifierconstants |
| 44 | + |
| 45 | +[Storyboards](https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/Storyboard.html) are great. They let you visually lay out your interface and define the interactions between different screens in a way that's much more compact than writing a ton of view code. |
| 46 | + |
| 47 | +At least I think so. But if you go and search for more information on storyboards, one of the first things you'll find is this Stack Overflow answer: |
| 48 | + |
| 49 | +[](http://stackoverflow.com/a/19457257/6010) |
| 50 | + |
| 51 | +We already fixed the part about code reuse with`objc-colordump`, and now we can fix the parts about failures at runtime. If we generate some code to turn your string identifiers into compiler-checked constants, we can avoid a whole class of problems. |
| 52 | + |
| 53 | +###Usage |
| 54 | + |
| 55 | +Call`objc-identifierconstants` with the`.storyboard` paths as arguments from the directory into which it should output the code. |
| 56 | + |
| 57 | +For a storyboard named "Foo" with string identifiers "Bar" and "Baz" somewhere in it, you'll get`FooStoryboardIdenfitiers.h` and`FooStoryboardIdentifiers.m` with`extern NSString *const FooStoryboardBarIdentifier` and`extern NSString *const FooStoryboardBazIdentifier` in it. Put them in your DerivedSources folder and you're good to go. |
| 58 | + |
| 59 | +##Command-line options (common to all three tools) |
16 | 60 |
|
17 | 61 | Usage:
|
18 |
| -*`objc-assetgen [-o <path>] [-f <path>] [-p <prefix>] [<paths>]` |
19 |
| -*`objc-assetgen -h` |
| 62 | +*`objc-assetgen [-6] [-o <path>] [-f <path>] [-p <prefix>] [<paths>]` |
| 63 | +*`objc-colordump [-6] [-o <path>] [-f <path>] [-p <prefix>] [<paths>]` |
| 64 | +*`objc-identifierconstants [-6] [-o <path>] [-f <path>] [-p <prefix>] [<paths>]` |
20 | 65 |
|
21 | 66 | Options:
|
22 | 67 | <dl>
|
| 68 | +<dt><code>-6</code></dt><dd>Target iOS 6 in addition to iOS 7</dd> |
23 | 69 | <dt><code>-o<path></code></dt><dd>Output files at <code><path></code></dd>
|
24 |
| -<dt><code>-f<path></code></dt><dd>Search for*.xcassets folders starting from <code><path></code></dd> |
| 70 | +<dt><code>-f<path></code></dt><dd>Search forinput files starting from <code><path></code></dd> |
25 | 71 | <dt><code>-p<prefix></code></dt><dd>Use <code><prefix></code> as the class prefix in the generated code</dd>
|
26 | 72 | <dt><code>-h</code></dt><dd>Print this help and exit</dd>
|
27 |
| -<dt><code><paths></code></dt><dd>Input files; this and/or-f are required.</dd> |
| 73 | +<dt><code><paths></code></dt><dd>Input files; this and/or`-f` are required.</dd> |
28 | 74 | </dl>
|
| 75 | + |