• Stars
    star
    322
  • Rank 130,398 (Top 3 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 9 years ago

Reviews

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

Repository Details

Easily manage themes and respond to theme changes by updating views in real time.

RNThemeManager

This small utility makes theming your iOS apps clean and simple. It was originally created so that I could have themable NIBs that utilized notifications to update view properties such as fonts and colors. I pushed beyond NIB support so that however you create your views, it will respond to your themes.

I also wanted to create a library that could be used by people that aren't app developers, i.e. designers. As long as someone has Xcode installed, they can easily make edits to your theme Plists without slowing down the development process.

Installation

Cocoapods

The preferred method of installation is with Cocoapods. The latest version is 0.1.0.

If you do not wish to use Cocoapods (and you really should), you can manually install RNThemeManager by just dragging and dropping all of the source files into your project. There are no framework dependencies.

Setup

The only required setup is that you create a Plist file named default.plist. The root of this file should be a dictionary. Each key represents the name of an attribute. Values should be either the name of a font, the size of a font, or a hex color code.

Fonts

View a list, and examples, of fonts included with iOS 6 here.

You can include custom fonts in your project and use them in your themes just as you would with a system font. For instructions on importing custom fonts see this Stackoverflow answer.

Font Sizes

RNThemeManager automatically builds your font and size based on the keyword for the font. Each font name must be accompanied by a font size key that is the font name suffixed with "Size". For example:

headerFont : ArialRoundedMTBold
headerFontSize : 24

Then when assigning a font key to a label (or any other view with text), the size will be automatically assigned.

- (UIFont *)fontForKey:(NSString*)key;

Colors

Colors are fairly simple. Just use a hexidecimal color code for the key value. There is no need to prefix with #.

- (UIImage *)imageForKey:(NSString *)key;

Images

Just like [UIImage imageNamed:@"name"], simply assign the image name of your asset as the value for an image key.

- (UIImage *)imageForKey:(NSString *)key;

Inheriting theme values

If you would like to use a single value as multiple keys you may, just set the value of a key to another key.

headerFont : Helvetica
headerFontSize : 20
backgroundColor : ffffff
redColor : d81417
headerColor : backgroundColor
headerButtonColor : headerColor
cellHeaderFontSize : headerFontSize
buttonBackgroundColor : redColor
// etc

Theming with NIBs

There are three steps to applying themes within NIBs. All of this is done in the Identity Inspector (⌥ ⌘ 3).

  1. Class a view as a respective RNTheme* subclass. You can subclass any of the RNTheme* classes as well.
  2. Setup keyPath keys that match the RNTheme* subclass.
  3. Set the values to said keyPaths to the keys you defined in your theme plists.

Sorry if that's a little confusing. Here are some pictures.

default.plist

RNThemeButton.h

@interface RNThemeButton : UIButton
<RNThemeUpdateProtocol>

// available theme keys
@property (nonatomic, strong) NSString *backgroundImageKey;
@property (nonatomic, strong) NSString *backgroundColorKey;
@property (nonatomic, strong) NSString *fontKey;
@property (nonatomic, strong) NSString *textColorKey;
@property (nonatomic, strong) NSString *highlightedTextColorKey;

@end

MainStoryboard.storyboard

Theming with Code

// optional: conform your View or ViewController to provided protocol
@interface MYViewController : UIViewController
<RNThemeUpdateProtocol>

// in -viewDidLoad (remember to removeObserver in -dealloc)
[[NSNotificationCenter defaultCenter] addObserver:self action:@selector(applyTheme) withObject:nil];

// in -viewWillAppear (or where you do your layout bits)
[self applyTheme];

- (void)applyTheme {
    // these objects do _not_ need to be RNTheme* classes/subclasses
    self.view.backgroundColor = [[RNThemeManager sharedManager] colorForKey:@"backgroundColor"];
    self.textField.font = [[RNThemeManager sharedManager] fontForKey:@"textFieldFont"];

    // example of custom theming
    self.textField.layer.cornerRadius = [RNThemeManager sharedManager].styles[@"cornerRadius"].floatValue;
}

Now whenever your theme file is changed the ViewController will automatically restyle your views based on your theme's setup.

Using Multiple Themes

To change the active theme, just call the following method:

[[RNThemeManager sharedManager] changeTheme:@"lowcontrast"];

Just make sure you have a plist with whatever theme name you provide.

Updating Views

All RNTheme* subclasses subscribe to notifications when a theme is changed and conform to a custom protocol (that only exists for semantics) called RNThemeUpdateProtocol.

If you wish not to use any of the RNTheme* views (and you certainly do not need to), you can update your views or even view controllers by listening for the following notification:

RNThemeManagerDidChangeThemes

See the above example of Theming With Code for how to implement.

Contact

License

RNThemeManager is a work from Ryan Nystrom under the MIT license. See the license doc for details.

More Repositories

1

RNFrostedSidebar

A Control Center-esque control with blurred background and toggle animations.
Objective-C
2,125
star
2

RNGridMenu

A grid menu with elastic layout, depth of field, and realistic animation.
Objective-C
1,282
star
3

RNBlurModalView

Add depth to your alerts.
Objective-C
830
star
4

RNRippleTableView

A custom table view with highly detailed ripple animations.
Objective-C
390
star
5

RNSwipeViewController

Swipe in view controllers from off-screen similar to Check the Weather.
Objective-C
293
star
6

HackerNewsReader

A small, read-only app for Hacker News.
Objective-C
261
star
7

WeatherKit

A quick and elegant solution to gathering local weather data.
Objective-C
166
star
8

UITableViewController-Containment-Demo

This is a sample project showing how to contain a UITableViewController in another VC with a Search Bar + Display Controller working as if it were all in a UITableViewController.
Objective-C
135
star
9

RNExpandingButtonBar

iOS UI widget that mimics the famous button used by the app Path.
Objective-C
126
star
10

RNTextStatistics

A category on NSString to gather advanced statistics on text.
Objective-C
124
star
11

RNBoilerplate

A bundle of my necessary frameworks, libraries, helpers, and settings for each Xcode project with configurable options.
Objective-C
106
star
12

SimpleWeather

A personal weather app I'm building in the open.
Swift
83
star
13

RNSwipeBar

Custom iOS widget to swipe a tab bar from the bottom of the screen
Objective-C
66
star
14

RNAvatarLogin

A simple way to autocomplete Gravatar icons for text fields.
Objective-C
57
star
15

Swift-CoreData

An extremely simple Core Data backed Swift app
Swift
55
star
16

PopDemos

Some demos of Facebook's Pop library made for a RayWenderlich.com tech talk.
Objective-C
53
star
17

TransitionExample

Testing UIViewControllerAnimatedTransitioning
Objective-C
42
star
18

World-Cup-Matches

Very simple app to keep an eye on World Cup 2014 matches.
Objective-C
39
star
19

Metro-Lights

A dark Xcode theme inspired by City Lights for Atom.
34
star
20

IGListKit-Binding-Guide

Starter and finished project for binding guide.
Objective-C
33
star
21

Xcode-Empty-Application

Adding the Empty Application template back to Xcode
30
star
22

UISVC-Compatibility-Demo

UISplitViewController setup that works on iOS 7 with all devices (iPhones + iPads).
Swift
30
star
23

VectorKit

A vector library and playground
Swift
29
star
24

D3Kit

An easy to use iOS library to access the Diablo 3 API.
Objective-C
28
star
25

OneNews-Swift

A work-in-progress universal iOS app for reading Hacker News, Designer News, and Product Hunt.
Objective-C
26
star
26

From-Scratch-Guides

Collection of guides & tutorials about building software from nothing.
25
star
27

WatchKit-by-Tutorials

Repository for all sample code in the upcoming WatchKit by Tutorials book from Ray Wenderlich.
Objective-C
20
star
28

IGListKit-Benchmarks

Benchmark tests for IGListKit
Swift
17
star
29

RNMDParser

Sample application experimenting with Obj-C Markdown parsing.
Objective-C
16
star
30

BottleRocket

An experimental CLI tool for generating normalized Swift models from example JSON responses.
Swift
12
star
31

New-Awesome-Blog

Showing off my awesome blog!
HTML
11
star
32

leadership

[WIP] My thoughts, principles, and resources on leadership.
11
star
33

NSString-RNSynonyms

A fast and lightweight solution to finding synonyms for words, built as a category on NSString.
Objective-C
11
star
34

RNTableView

A recreation of UITableView with only the bear essentials. Use this to start a highly customized UITableView.
Objective-C
11
star
35

WatchKit-Threading

A really simple demo app showing that threading in WatchKit works.
Objective-C
10
star
36

Arduino-HomeKit

HTML
9
star
37

RWDevCon-View-Controller-Transitions

Objective-C++
8
star
38

RNCardStreamController

A vertical section and cell navigation control inspired by Facebook Paper.
Objective-C
8
star
39

Armory-for-Diablo-3

Source code for my Armory app that is awaiting Apple approval.
Objective-C
7
star
40

WeatherExample

Building a Weather app example using ReactiveCocoa for raywenderlich.com
Objective-C
6
star
41

status-checklists

Action to that updates Issue state with checklists
TypeScript
5
star
42

create-child-issues

Action to generate child issues with labels.
JavaScript
5
star
43

Crateless

https://itunes.apple.com/us/app/crateless/id563997439?ls=1&mt=8
Objective-C
5
star
44

.github-template

4
star
45

Playground

Link: https://github.com mention: @rnystrom
3
star
46

RNNumberedTextView

Easily add customizable line numbers to UITextView.
Objective-C
2
star
47

issue-actions

WIP actions to sync parent and child issues.
JavaScript
2
star
48

M3Demo

Objective-C
2
star
49

HackerNews-iOS

Objective-C
1
star
50

Blog

Nothing special.
CSS
1
star
51

dotfiles

1
star
52

Read-Write-Protection-Perf

Teeny tiny profiling of different ways to protect mutations in your objects.
Objective-C
1
star
53

SunlightTest

Sample app to test background and text colors in sunlight
Objective-C
1
star