Global Styles
Wow, this discussion board is full of questions and devoid of answers. :) Here's hoping someone can help.
Is there a way to create a single label style and have its style declarations propagate to all labels in your app? I feel like I'm writing a lot of redundant code.
Here's a simple example. Let's say I have two white labels:
var labelOne = Titanium.UI.createLabel({
color:'#FFF',
text:'Label One',
});
var labelTwo = Titanium.UI.createLabel({
color:'#FFF',
text:'Label Two',
});
Is there a way to define that text color once for both labels? You can imagine if I have 20 form fields in an app with 10 style definitions each, it feels sloppy to define them all over again for each one.
Any ideas?
4 Answers
-
Accepted Answer
You can create your own wrapper for creating the label and apply both the system style and the global style.
Using array_merge from: http://phpjs.org/functions/array_merge:326
var globalLabelStyle = {"color": "#0f0", "fontSize":16}; function createLabel(style) { // The 2nd array overrides that in the first style = array_merge(globalLabelStyle, style); return Titanium.UI.createLabel(style); } // Default style applied var defaultLabel = createLabel({"text":"default"}); var thisLabel = createLabel({"color":'#f00',"text":"overridden"});
As Randy suggested you can store the globalLabelStyle in app properties.
-
ya this is nuts, can you create a wrapper function/class over createLabel that would basically append its passed in args with your global style args and call createLabel?
-
You can probably do this with Titanium.App.Properties
See: http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.App.Properties-module
-
great idea using array_merge, thanx for sharing this information