Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Appcelerator Titanium Smartphone App Development Cookbook Second Edition
  • Table Of Contents Toc
Appcelerator Titanium Smartphone App Development Cookbook Second Edition

Appcelerator Titanium Smartphone App Development Cookbook Second Edition

By : Jason Kneen
5 (4)
close
close
Appcelerator Titanium Smartphone App Development Cookbook Second Edition

Appcelerator Titanium Smartphone App Development Cookbook Second Edition

5 (4)
By: Jason Kneen

Overview of this book

This book will take you through the process of building cross-platform, native UI applications for the mobile from scratch. You will learn how to develop apps, how to use GPS, cameras and photos and how to build socially connected apps. You will also learn how to package them for submission to the App Store and Google Play. This cookbook takes a pragmatic approach to creating applications in JavaScript from putting together basic UIs, to handling events and implementation of third party services such as Twitter, Facebook and Push notifications. The book shows you how to integrate datasources and server APIs, and how to use local databases. The topics covered will guide you to use Appcelerator Studio tools for all the mobile features such as Geolocation, Accelerometer, animation and more. You’ll also learn about Alloy, the Appcelerator MVC framework for rapid app development, and how to transfer data between applications using URLSchemes, enabling other developers to access and launch specific parts of your app. Finally, you will learn how to register developer accounts and publish your very own applications on the App Store and Google Play.
Table of Contents (16 chapters)
close
close
15
Index

Creating and formatting labels

Whether they are for presenting text content on the screen, identifying an input field, or displaying data within a tablerow, labels are one of the cornerstone UI elements that you'll find yourself using all the time with Titanium. Through them, you'll display the majority of your information to the user, so it's important to know how to create and format them properly.

In this recipe, we'll create three different labels, one for each of the input components that we'll be adding to our app later on. Using these examples, we'll explain how to position your label, give it a text value, and format it.

How to do it...

Open up your app.js file, and put these two variables at the top of your code file, directly under the tabgroup creation declaration. These are going to be the default values for our interest rate and loan length for the app:

//application variables
var numberMonths = 36; //loan length
var interestRate = 6.0; //interest rate

Let's create labels to identify the input fields that we'll be implementing later on. Type the following source code into your app.js file. If you are following along with the LoanCalc sample app, this code should go after your imageview logo, added to the view from the previous recipe:

var amountRow = Ti.UI.createView({
  top: 10,
  left: 0,
  width: Ti.UI.FILL,
  height: Ti.UI.SIZE
});

//create a label to identify the textfield to the user
var labelAmount = Ti.UI.createLabel({
  width : Ti.UI.SIZE,
  height : 30,
  top : 0,
  left : 20,
  font : {
    fontSize : 14,
    fontFamily : 'Helvetica',
    fontWeight : 'bold'
  },
  text : 'Loan amount:   $'
});

amountRow.add(labelAmount);

view.add(amountRow);

var interestRateRow = Ti.UI.createView({
  top: 10,
  left: 0,
  width: Ti.UI.SIZE,
  height: Ti.UI.SIZE
});

//create a label to identify the textfield to the user
var labelInterestRate = Ti.UI.createLabel({
  width : Ti.UI.SIZE,
  height : 30,
  top : 0,
  left : 20,
  font : {
    fontSize : 14,
    fontFamily : 'Helvetica',
    fontWeight : 'bold'
  },
  text : 'Interest Rate:  %'
});

interestRateRow.add(labelInterestRate);

view.add(interestRateRow);

var loanLengthRow = Ti.UI.createView({
  top: 10,
  left: 0,
  width: Ti.UI.FILL,
  height: Ti.UI.SIZE
});

//create a label to identify the textfield to the user
var labelLoanLength = Ti.UI.createLabel({
  width : 100,
  height : Ti.UI.SIZE,
  top : 0,
  left : 20,
  font : {
    fontSize : 14,
    fontFamily : 'Helvetica',
    fontWeight : 'bold'
  },
  text : 'Loan length (' + numberMonths + ' months):'
});

loanLengthRow.add(labelLoanLength);

view.add(loanLengthRow);

How it works...

By now, you should notice a trend in the way in which Titanium instantiates objects and adds them to views/windows, as well as a trend in the way formatting is applied to most basic UI elements using the JavaScript object properties. Margins and padding are added using the absolute positioning values of top, left, bottom, and right, while font styling is done with the standard font properties, which are fontSize, fontFamily, and fontWeight in the case of our example code.

Here are a couple of important points to note:

  • The width property of our first two labels is set to Ti.UI.SIZE, which means that Titanium will automatically calculate the width of the Label depending on the content inside (a string value in this case). This Ti.UI.SIZE property can be used for both the width and height of many other UI elements as well, as you can see in the third label that we created, which has a dynamic height for matching the label's text. When no height or width property is specified, the UI component will expand to fit the exact dimensions of the parent view or window that encloses it.
  • You'll notice that we're creating views that contain a label each. There's a good reason for this. To avoid using absolute positioning, we're using a vertical layout on the main view, and to ensure that our text fields appear next to our labels, we're creating a row as a view, which is then spaced vertically. Inside the row, we add the label, and in the next recipes, we will have all the text fields next to the labels.
  • The textAlign property of the labels works the same way as you'd expect it to in HTML. However, you'll notice the alignment of the text only if the width of your label isn't set to Ti.UI.SIZE, unless that label happens to spread over multiple lines.
    How it works...
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Appcelerator Titanium Smartphone App Development Cookbook Second Edition
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon