Position text inside a label (Titanium Mobile)

You must Login before you can answer or comment on any questions.

Is there a way to control the vertical alignment of text inside a label, e.g. say your label is 300px in height, but you want the text to start at 20px from the top edge.

Is there any way to control this?

Thanks!

— asked 3 years ago by HOWEST *
0 Comments

3 Answers

I noticed there are TEXT_VERTICAL_ALIGNMENT_TOP,.._BOTTOM,... constants in the base Titanium.UI library, but what property can I couple it with to achieve the alignment or is this not possible within a label?

This worked for me, but it won't cut the text if it is longer (higher) then the height of the View:

this.infoContainer = Titanium.UI.createView({
    top : 19,
    left    : 62,
    width   : 238,
    height  : 28
});
 
this.infoLabel = Titanium.UI.createLabel({
    height  : 'auto',
    top : 0
});
 
this.infoContainer.add(this.infoLabel);
this.view.add(this.infoContainer);
Result: Top-Vertical-Align of a text in a two line height element. But the label will no longer truncate (and add a trailing '...') the text when the text is longer (higher).

Any tricks to have top-align and truncate of the text?

— answered 3 years ago by Udo Trappe
answer permalink
1 Comment
  • The problem is that height:'auto' precludes truncation.

    Here is one idea.

    Add a custom event listener to the label (e.g. refresh) which you can fire immediately after you change the label text.

    Then, in the listener:

    if(this.infoLabel.size.height > 28)
    {
        this.infoLabel.height = 28;
    }
    else
    {
        this.infoLabel.height = 'auto';
    }

    — commented 3 years ago by James K

I am interested in this, too. Even did not find an example in KitchenSink.

Your Answer

Think you can help? Login to answer this question!