The JSON file, theme.json, is used as the single styling source for all UI elements in your app. The following is an example of the basic format of this file:

[
// a list of resource names and values.  Can contain any number of entries.
{
"resourceName" : "@type:resourceValue",
"secondResourceName" : "@type:secondResourceValue"
},


// a list of styles
{
"styleName"  : {
    "stylePropery" : "@type:stylePropertyValue",
    "styleProperty:withTwoArgs" : ["@type:arg1", "@type:arg2"],
    "layer.property" : "@type:stylePropertyValue"
  }
}

]

The two key elements of the theme file are an array. The resource list and style list are both arrays.

The Resource List

The resource list is simply a list of resource names and resource values. The resource name can be anything, but should begin with a lower-case character, and resource names containing more than one word should be in medial caps. Resource values take this format:

@type:value

Where @type: is a prefix stating the type of the value and value is a string whose format is specific to the type. A list of valid types and values is shown in the following table.

Type

Explanation

Example

color

Defines a color as space-separated RGBA float values, in the range of 0 - 1.

It is not necessary to prefix the floats with @float:

@color:0.5 0.5 1 0.77

font

Creates a font using the font name and font size. The font name and size must be colon-separated values.

@font:Helvetica-Bold:15

unsigned int

Specifies an unsigned int value. In addition to setting normal integer values, a common pattern is to define an unsigned-int resource in the resource list and then reference that resource in multiple styles. This is particularly useful for enums, which only need to be defined once.

@uint:3

image

Creates a UIImage with the supplied name. The image created is chosen according the device's type and resolution, as normal.

@image:search-button-normal

float

Creates a float with the supplied value.

@float:0.8765

nil

This can be used to set a value to nil. Any unrecognized @type string will be converted to nil. There is no special handling of @nil, but for consistency, @nil should always be used to set a value to nil.

@nil

colorref

Gets the CGColorRef from a UIColor object. This can be useful when using the CALayer object on a view, which uses CGColorRef values instead of UIColor values. As shown in the example, the @colorref: tag expects to be passed an @color type, either a directory, or from a resource.

@colorref:@resource:blueColor\\

@colorref:@color:0.5 0.5 1 1

edge_insets

Specifies the values that should be used to create a UIEdgeInsets struct.

@edge_insets:0.5 0.5 0.5 0.5

resource

This is not a type on its own.

Its value should match the name of a resource in the resource list, resulting in using that resource's value wherever the resource name is referenced by the @resource type. It cannot be used to have a resource to point to another resource.

@resource:resourceName

The Style List

The second element in the theme file's root array is the style list. The style list is a list of style keys and values. Each style has one key and one value dictionary. For example:

"UITextView" : {
      "textColor" : "@resource:blackColor",
      "backgroundColor" : "@resource:primaryColor",
      "alpha" : "@float:1.0"
}

In this example, UITextView is the style key, and the text inside and including the curly braces is the value dictionary. The style key can take three different formats, detailed in the following table:

Type

Explanation

Example

View

This is a string that directly matches the name of a UIView subclass. When using this type of style, only properties marked as UIAppearance compatible can be used since UIKit Appearance Proxy function is used. A style created this way will automatically be applied to any new instances of the specified class, without any additional developer work.

UITextView

View when used inside a view or views

This is used the same as View, except that the string that matches the UIView subclass is followed by the '>' character and then another UIView subclass.

This means that the style is only applied whenever the first view is inside the second view. In the example, the style will be applied to UILabel instances, but only when they're contained within a UIButton view.

UILabel>UIButton

Style ID

To distinguish a style ID from a style that maps directly to a view, it is prefixed with the tilde(~) character. The rest of the string must be unique in the file. A style that has a style ID is not applied automatically - it must be manually applied the style to a view.

This type of style uses the Appearance Proxy feature and does not need to be limited to calling selectors that are marked as appearance proxy compatible. Any properties can be set, and any 'set' selectors can be called, so long as the types expected are supported.

~searchBoxStyle


Copyright © 1997, 2013 Oracle and/or its affiliates. All rights reserved. Legal Notices