Home

Awesome


Button widget Titanium Alloy

This widget for the Appcelerator Titanium Alloy MVC framework provides a view that mimics Ti.UI.Button, but with more flexible styling and support for icon fonts.

demo

The repository

The repository contains two branches. This master branch contains the widget. The other test branch has a complete Titanium Alloy demo/test project.

Quick Start

Get it gitTio

Download this repository and consult the Alloy Documentation on how to install it, or simply use the gitTio CLI:

$ gittio install nl.fokkezb.button

Use it

<Widget src="nl.fokkezb.button" onClick="myCallback" icon="icon-on" title="My title" style="ios" />`

If you want to use icons:

See the reference way down this document for all available properties.

Pre-defined styles

The widget comes with pre-defined styles that can be applied to a button just the style property, either in <Widget /> or via TSS.

For example:

<Widget src="nl.fokkezb.button" title="Primary" style="bs-primary" />
StyleExampleDescription
iosiosLike Ti.UI.Button on iOS
bs-defaultbs-defaultTwitter Bootstrap's default button
bs-primarybs-primaryTwitter Bootstrap's primar button
bs-infobs-infoTwitter Bootstrap's info button
bs-successbs-successTwitter Bootstrap's success button
bs-warningbs-warningTwitter Bootstrap's warning button
bs-dangerbs-dangerTwitter Bootstrap's danger button
bs-inversebs-inverseTwitter Bootstrap's inverse button
bs-linkbs-linkTwitter Bootstrap's link button

Adding styles and setting the default

Add styles by requiring the widget's styles-lib in your app's alloy.js and use the set() method to set (or override) a pre-defined style like this:

var Styles = require('nl.fokkezb.button/styles');
Styles.set('my-style', {
	borderWidth: 1,
	borderColor: '#000',
	backgroundColor: '#ccc',
	activeStyle: {
		backgroundColor: '#000',
		color: '#fff'
	}
});

The lib also exposes has() and get(). Use the latter to get and extend an existing style before overwriting it using set().

You can also set the default style to be used for any button that doesn't specify a style itself. On iOS the ios style is the default. Change it in your app's alloy.js like this:

var Styles = require('nl.fokkezb.button/styles');
Styles.setDefault('bs-default');

An example can be found in the demo app's app/alloy.js file.

TSS styling

Of course you can also style the widget like any other view using the TSS file of the controller-view where your require the widget. Just set a class or id to the <Widget /> and style it like this:

"#myButton": {
	borderWidth: 1,
	borderColor: '#000',
	backgroundColor: '#ccc',
	activeStyle: {
		backgroundColor: '#000',
		color: '#fff'
	},
	iconFont: 'FontAwesome',
	icon: Alloy.Globals.fa.powerOff
}

Run-time styling

You can use $.myId.applyProperties() to apply any new properties to the widget after it has been automatically initialized:

$.myId.applyProperties({
	color: 'red'
});

Event listeners

Listen to the click-event like you would do with a regular button.

Either in <Widget />:

<Widget onClick="myCallback" title="My title" id="myId" />

Or in the requiring controller:

$.myId.addEventListener('click', myCallback);

The callback receives an Object containing 2 properties:

An example can be found in the demo app's app/controllers/index.js file.

Since 1.3, when listening to a parent of the button, the e.source will also contain all non-object properties defined for the widget via XML or TSS.

Property reference

To understand what properties are available and what they do, you need to understand how the button is constructed. The next illustration shows the outer (purple), inner (red), icon (blue) and title (green) elements and where padding and spacing is applied.

elements

Widget-specific properties

PropertyTypePurpose
enabledBooleanIf false, the disabledStyle will be applied and no events are triggerd
titleStringOptional text for the title of the button
titleidStringOptional textid for an internationalized title of the button
iconStringOptional icon image file name or the text/unicode if you use an icon font.
iconFontStringThe font family to use for the icon.
iconSizeNumberFont size for the icon, overruling the button fontSize
iconPositionStringPositions the icon to the left (default) or the right of the title
paddingNumber/String/ObjectPadding between the outer and the inner. Either pass one dimension, a list of 1 to 4 dimensions or an object containing one of top, right, bottom and left as keys and their dimension as values. Don't use padding if you specify a width and height and want the inner to be centered in the outer.
spacingNumber/StringSpacing between the icon and the title
styleStringPre-defined style to load before applying any of the common properties in the next table.
activeStyleObjectObject containing any of the common properties listed in the next table that will be applied while the user presses the button.
disabledStyleObjectObject containing any of the common properties listed in the next table that will be applied if the button is not enabled.

Titanium-common properties

PropertyTypeApplied toComments
width, heightNumber/Stringouter
top, right, bottom, leftNumber/Stringouter
centerPointouter
borderWidthNumber/Stringouter
borderColorStringouter
borderRadiusNumber/Stringouter
backgroundColorNumber/Stringouter
backgroundGradientGradientouter
backgroundImageStringOuter
backgroundTopCapNumberOuter
backgroundLeftCapNumberOuter
backgroundRepeatBooleanOuter
colorStringtitle, icon
shadowOffsetObjecttitle, icon
shadowColorStringtitle, icon
fontFonttitle, iconFor the icon, font.fontFamily is ignored and font.fontSize can be overrulled by iconSize
textAlignStringtitleOnly when width is set
verticalAlignStringtitleOnly when height is set
opacityNumberouter
visibleBooleanouter
bubbleParentBooleanouter

Public interface

MethodDescription
applyProperties()Main method for setting up and changing the button
show()Shows button
hide()Hides button
setIcon([icon], ]iconFont])Changes the icon
getIcon()Returns an { icon:, iconFont } object
setTitle(title)Changes the title
getTitle()Returns the title
setTitleid(titleid)Changes the titleid
getTitleid()Returns the titleid
setStyle(style)Applies the style
getStyle()Returns the style name
setAccessibilityLabel(label, hint)Sets the label to be read by the accessibility feature of the device (and hint sentence, optional). Useful for icon-only buttons
addEventListener(event, callback)Attaches event listener
removeEventListener(event, callback)Removes event listener
fireEvent(event, dictionary)Fires event on the button
idHolds the id of the widget, so you have this in events

Ideas for improvement

Feel free to help me improve this widget by forking and submitting pull requests or issues with more ideas. Here's my whishlist:

Changelog

Licenses

This project is licensed under the Apache Public License (Version 2). Please see the LICENSE.txt file for the full license.

The Font Awesome font is licensed under the SIL Open Font License. The Font Awesome pictograms are licensed under the CC BY 3.0 License. Attribution is no longer required in Font Awesome 3.0, but much appreciated.

You can get Ligature Symbols for free. This Font is licensed under the SIL Open Font License for download and using. Ligature Symbols has broad support for the modern browser (Chrome, Safari, Firefox, iOS - Mobile Safari, Android Browser).

Appcelerator, Appcelerator Titanium and associated marks and logos are trademarks of Appcelerator, Inc.

Titanium is Copyright (c) 2008-2012 by Appcelerator, Inc. All Rights Reserved.

Titanium is licensed under the Apache Public License (Version 2). Please see the LICENSE file for the full license.