tombenner / nui

Style iOS apps with a stylesheet, similar to CSS
MIT License
3.76k stars 460 forks source link

Add a method for getting colors and fonts from the NSS #171

Open fpaaske opened 11 years ago

fpaaske commented 11 years ago

When using 3rd party components that supports UIAppearance (e.g. SIAlertView), it would be nice to do something like

[NUISettings setGlobalExclusions:@[@"SIAlertView"]];

then

[[SIAlertView appearance] setButtonColor:[NUIAppearance getColor:@"primaryButtonColor"]; [[SIAlertView appearance] setCancelButtonColor:[NUIAppearance getColor:@"cancelButtonColor"];

nitriques commented 10 years ago

Try NUISettings for getting values for a class for now.

fpaaske commented 10 years ago

Ah, but If I was to use this I'd need to create a class in the nss and add a property for each nss variable I need. It doesn't sound awesome. I'd like to access the variables directly.

Anyway, if anyone has the time I'd appreciate a method for getting color by variable name, not class property. Until then I'll just use NUISettings. Thanks!

nitriques commented 10 years ago

I'd like to access the variables directly.

I think you missunderstand how this works. Variables are just placeholders, they are not real obj-c variables. The .nss file is just like a plist file, it's text, and it does not gets compiled. NUISettings is the only way to go.

fpaaske commented 10 years ago

No, I understand that the @-variables in the nss file are just placeholders, but at some point they are parsed and substituted within the nss-class blocks. At some time during that process, they could be loaded into a map/dictionary for manual retrieval later. It makes no sense to manually load a resource for a specific nss-class (e.g. SomeButton) to use it in a completely different UI element (e.g. SIAlertView's button number three).. The only way that makes sense is if you make a nss-class for the specific resource, then load and apply it manually. But that feels wrong, that you have the class, but must apply it manually.

nitriques commented 10 years ago

It makes no sense to manually load a resource for a specific nss-classs

I had no such problem. The NUISettings contains a dirtionary that you can read via the class name. The file is parsed only once and you read from that everytime.

If you only want to access specific variables values, just create a nui-class that contains those values. But usign the nuiClass property is much cleaner.

fpaaske commented 10 years ago

What do you mean by 'using the nuiClass property'? Can you give a small example of how you'd do it? Maybe I'm totally missing the point here :)

nitriques commented 10 years ago
self.view.nuiClass = @"MyClass";

In your controller. You need to import UIView+NUI.h file

See this section of the README: https://github.com/tombenner/nui#setting-an-elements-style-class-programmatically

fpaaske commented 10 years ago

Ok, I wasn't missing the point after all :-)

Yes, I do this for the UI elements I have control over. But sometimes I need to style 3rd party UI elements (like SIAlertView, which supports UIAppearance). For that I can't set the nuiClass anymore, at least not without inspecting/modifying their source code. This is the particular use case that triggered this request.

To achieve this in today's NUI I'd have to create a class in nss, add all the styling I need

@alertDestructiveButtonColor: #ff0000;
...
SIAlertViewHolder {
    destructive-button-background-color: @alertDestructiveButtonColor;
    ...
}

then for each styling parameter, use [NUISettings getColor: withClass:] to get the resource and use it with the correct UIAppearance method.

UIColor *destructiveColor = [NUISettings getColor:@"destructive-button-background-color" withClass:@"SIAlertViewHolder"];
[[SIAlertView appearance] setDestructiveButtonColor:destructiveColor];

It's not totally bad, but it adds overhead and complexity to the code and its maintainability.

I'd still like to do it like this:

[[SIAlertView appearance] setDestructiveButtonColor:
    [NUISettings getColor:@"alertDestructiveButtonColor"]];

It is cleaner, more intuitive and has much less config/nss (only one string that can be mistyped).

dmi3j commented 10 years ago

Having something like:

[NUISettings getColorByName:@"someCustomColorName"]];

would just awesome.

ArniDexian commented 7 years ago

Getting values of defined variables (in .nui file) is convenient. I think that stuff should be in NUI library