0

Currently i'm using UISS to change some styles of my app, but I think its not possible to style specific controls. Eg. If I want to change the image of a button with tag 1 or with the id 'xpto'. Is there any way to do this with UIAppearance or with UISS?

I have a button like:

UIButton *xpto = [[UIButton alloc] init];

and I want to theme it when a method is called, at the moment I'm using UISS to theme my objects with

-(void) changeTheme:(NSString*)themeName{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:themeName ofType:@"json"];
    AppDelegate *dlg =[[UIApplication sharedApplication] delegate];
    dlg.uiss.style.url = [NSURL fileURLWithPath:filePath];
    [dlg.uiss reloadStyleAsynchronously];
}

json file:

{
    "UIButton": {
        "titleColor:normal": "red"
     }
}

My only issue is that this changes all the UIButtons and I only want to change xpto. Is there any way to define something like bellow to only change this button.

    "UIButton#xpto": {
        "titleColor:normal": "red"
     }
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
fnxpt
  • 434
  • 3
  • 13
  • 3
    post some code dude what you have tried ? – Rahul Vyas Jul 09 '13 at 14:21
  • 1
    They do it pretty well for this https://github.com/tombenner/nui – Popeye Jul 09 '13 at 20:54
  • My only issue with nui is that you have to specify all the controls you want to render... If I want to change the theme I have to say that control1, 2 and 3 needs to rerender, and as far as I know its not possible to make complex things like change a UIButton image with edges... – fnxpt Jul 10 '13 at 10:32

1 Answers1

2

You can do this by creating classes at runtime.

First, define this function somewhere that can be seen from your code:

Class SGBCreateClassWithId(Class baseClass, NSString *classId)
{
    NSString *className = NSStringFromClass(baseClass);

    if (classId.length > 0)
    {
        className = [NSString stringWithFormat:@"%@#%@", className, classId];
    }

    Class subClass = NSClassFromString(className);

    if (!subClass)
    {
        subClass = (Class)objc_allocateClassPair(baseClass, [className UTF8String], 0);
        if (subClass)
        {
            objc_registerClassPair(subClass);
        }
    }

    return subClass;
}

Now, change this line:

UIButton *xpto = [[UIButton alloc] init];

To this:

Class classWithId = SGBCreateClassWithId([UIButton class], @"xpto")
UIButton *xpto = [[classWithId alloc] init];

Now your button belongs to a subclass of UIButton called UIButton#xpto, so your UISS should work the way you want.

One caveat: UISS only recognises classes that exist at the time you load the UISS JSON file. So if you do create new classes this way, you need to reload your UISS JSON.

Simon
  • 25,468
  • 44
  • 152
  • 266