Tuesday 24 February 2015

Underline in Ti.UI.Label

index.js
function underline(_label, _word) {
    if (Ti.Platform.name === 'iPhone OS') {
        var text = _label.getText();
        var attr = Titanium.UI.iOS.createAttributedString({
            text : text,
            attributes : [{
                type : Titanium.UI.iOS.ATTRIBUTE_UNDERLINES_STYLE,
                value : Titanium.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
                range : [text.indexOf(_word), _word.length]
            }]
        });
        _label.setAttributedString(attr); // Why doesn't this work?
    }

    if (Ti.Platform.osname === 'android') {
        var text = _label.text.split(_word);
        _label.setHtml(text[0] + '<u>' + _word + '</u>' + text[1]);
        _label.setText(undefined);
    }
}


// Now you can do this:
var label = Ti.UI.createLabel({
    text: 'This is a label with an underlined word in its text.'
});
underline(label, "underlined");
win.add(label)
Reference

Saturday 14 February 2015

Use Titanium Studio without internet. ( Bypass Titanium studio authentication )


To avoid authentication of titanium studio and open it offline :


1. Locate TitaniumStudio.ini (How to)

2. Add the following at the end of the file:


-Dtitanium.bypassAuthentication=true

Thursday 12 February 2015

Custom Toast Titanium

index.js
var titost = require('titoast');
titost.setMessages("New message for toast");
titost.showOnView($.index);
titoast.js
var currentView;
var toastShown = false;
var toastmsg = 'toast message variable not set.';
var duration =3000;

var container = Ti.UI.createView({
    height : Ti.UI.SIZE,
    width : Ti.UI.FILL,
    bottom : '0dp',
});

var labelContainer = Ti.UI.createView({
    height : Ti.UI.SIZE,
    width : '280dp',
    borderRadius : 5,
    backgroundColor : "#000",
    opacity : 0,
    top : '10dp',
    bottom : '10dp'
});

var label = Ti.UI.createLabel({
    left : "10dp",
    right : "10dp",
    top : '10dp',
    bottom : '10dp',
    height : Ti.UI.SIZE,
    color : "#FFF",
    textAlign : "center",
    font : {
        fontSize : 13,
        fontWeight : "bold"
    },
});

labelContainer.add(label);
container.add(labelContainer);

exports.setMessages = function (msg) {
    toastmsg=msg;
};

exports.showOnView = function(view) {
    if (!toastShown) {
        toastShown = true;
        Ti.API.info('shown');
        currentView = view;
        label.text = toastmsg;
        open();
        setTimeout(function() {
            close();
        }, duration);
    }
};

function open() {   
    currentView.add(container);
    labelContainer.animate({
        opacity : 0.8,
        duration : 250
    });
};

function close() {
    labelContainer.animate({
        opacity : 0,
        duration : 250
    }, function(_event) {
        toastShown=false;
        currentView.remove(container);
        currentView = null;
    });
};