Giter Site home page Giter Site logo

awlthememanager's Introduction

AWLThemeManager

CocoaPods Compatible

AWLThemeManager is a lightweight theme manager for iOS.

Features

What can be customized

  1. Image;
  2. Font;
  3. Color;
  4. Property-list supported objects
  5. Any file in bundle.

Theme Inheritance

In actual work, there may be a little difference among themes. In a traditional way, you need to keep the same content in every theme. This is a bad way if you want to change the value, you need find all the places and change it. From now on, you can feel release if you want to do that because of the inheritance of AWLThemeManager. You just need to modify the root theme then all the themes that inheritant from the root theme will be changed to the new value.

How to use

Import to your poject

  • Use cocoapods

      pod 'AWLThemeManager'
    
  • Traditional way Just add the header and source file to your project

    • AWLThemeManager.h
    • AWLThemeManager.m

Write your own theme package

Bundle is preferred as theme package, though folder can also be used.

The structure of bundle file is like the follow screenshot:

Bundle

defaults.plist is necessary. Add other files to the root directory of bundle.

defaults.plist

defaults.plist is used to set the property of themes, include color, font etc.

defaults.plist

Base info

Default key used in defualts.plist:

  • AWL_THEME_NAME

    This key is necessary. it is the name of the theme, used to identify the theme, must be only.

  • AWL_BASE_THEME

    Alternative. If you want to use theme as your base theme, you should add this key. Set the name of base theme to this key.

Define Color

The format of color is :

defaults.plist

It will return the color use follow method:

[UIColor colorWithRed:[array[0] doubleValue]/255
                green:[array[1] doubleValue]/255
                 blue:[array[2] doubleValue]/255
                alpha:[array[3] doubleValue]];

Make sure you set the right value to the color.

Support white colorspace format. Colors can now be specified in white colorspace (e.g. 0.4,1).

Support color with pattern image. Colors can now be specified as a pattern image with the format "@[ImageName],[Optional Alpha]" (e.g. @Foo", "@Foo,0.5").

Support hex format. Specify colors in hex format as

#RGB
#ARGB
#RRGGBB
#AARRGGBB

Support reference. If you want to set the same value to different color key, you can set the key of one color to another color, then AWLThemeManager will find the actual value of the color.

Modify alpha by reference. If you reference a color, e.g. “COLOR1” = “255,0,0,1”, “COLOR2” = “COLOR1” you can now append “:[alpha]” to the reference to modify it’s alpha, e.g. “COLOR2” = “COLOR1:0.2” will equate to “255,0,0,0.2”.

Define Font

The format of font is :

defaults.plist

The first one is name of font, second is size of the font. It will return the font use follow method:

[UIFont fontWithName:fontName size:fontSize]

If you want to use system font, you should ignore the first value, like this:

,14       //systemFontOfSize
bold,14   //boldSystemFontOfSize
italic,14 //italicSystemFontOfSize

Support reference. Same with the color.

Define image

There are two way for adding img to theme bundle:

  1. Put the image file in the bundle;
  2. Add a xcassets file, then put image files in the xcassets.

For method 2, the bundle needs to be an actual OS X bundle target in the project with it's SDK set to iOS & a valid info.plist in order for Xcode to compile the .xcassets file inside it. Please refer to Assets.bundle in demo project.

Support reference. Same with the color. If image name isn't found in defaults.plist, we use key as image name.

File

Now you can have “foo”->”bar” in the theme and search all bundles for “foo” of type “dat”, in addition to falling back to the main bundle. If key “foo” is not found, “foo” is assumed to be the file name.

Other

You can add whatever you want to the defaults.plist. Just use objectForKey: to get the value.

How to use in your project

Add the absolute path of bundle to AWLThemeManager object, then set the current theme that you want. For example:

NSString *bundlePath1 = [[NSBundle mainBundle] pathForResource:@"BaseSample" ofType:@"bundle"];
NSString *bundlePath2 = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"bundle"];
AWLThemeManager *mgr = [[AWLThemeManager alloc] init];
[mgr addTheme:bundlePath1];
mgr.currentTheme = [mgr addTheme:bundlePath2];

Then you can access the resource use the same AWLThemeManager object as follow.

UIImage *img = [mgr imageNamed:@"icon"];
titleLabel.textColor = [mgr colorForKey:@"Content_Text_Color"];
titleLabel.font = [mgr fontForKey:@"Content_Font"];

awlthememanager's People

Contributors

neeeo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jrapoport

awlthememanager's Issues

Support keyed images

Currently you can override image "foo" (foo.png) in Bundle A with foo.png in Bundle B.

However, sometimes you need to override image "foo" (foo.png) in Bundle A with quack.png in Bundle B. Basically you want to request imageNamed: "Foo", but have the theme swap that out for "Quack" via the plist, and then try to find imageNamed: "Quack" instead.

Support compiled xcasset files

Add support for compiled xcasset files (Assets.car) in theme bundles.

Note:
The bundle needs to be an actual OS X bundle target in the project with it's SDK set to iOS & a valid info.plist in order for Xcode to compile the .xcassets file inside it

Add colorWithPatternImage support

Now if you specify as color as “#[ImageName]” it will search for the
“Foo” image using the usual methods & if found, return a color using
that image as a pattern. To change the alpha, use
“#[ImageName],[Alpha]” e.g. #Foo,0.3

see 63c7e3f

More precise colors

Increase the precision from float to double (CGFloat will define to double)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.