multiple labels in multiline

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

Hey guys, I'm using inside a table row, multiple labels, one next to the other, only two to be more precise, however, when the second label is bigger than the rest of the width, it breaks to the line below where the second label start, instead of the initial left position where the first label begins, if I put a third label, then it would start correctly, but not before, below is a screenshot of what is happening.

If the second label was too big, I wanted that the second label continued on the next line right below the first label.

multiline label

My code:

var rowView = Ti.UI.createView({
            width:'auto',
            height:35,
            top:5,
            left:60,
            layout:'horizontal'
 
        })
        var rowName = Ti.UI.createLabel({           
            width:'auto',
            top:0,
            left:0,
            text:response.close.users[i].name+" is shouting ",
            color:'#94A1AF',
            font:{fontSize:14,fontWeight:'normal'}
        });
        var rowShout = Ti.UI.createLabel({
            width:'auto',
            top:0,
            left:0,
            text:'"'+response.close.users[i].shout+'".',
            color:'#94A1AF',
            font:{fontSize:14,fontWeight:'bold'}
        });
 
        rowView.add(rowName);
        rowView.add(rowShout);
        row.add(rowView);

— asked 10 months ago by von Goethe
0 Comments

2 Answers

Hello Von,

use the static width for the label and set label height 30 so it will automatic continues at next line if more data is there.

— answered 10 months ago by Sarafaraz Babi
answer permalink
4 Comments
  • I tried putting static width on view and commenting on labels, nothing, vice-versa(the labels started in the second line with a width of 240), adding height to both labels, the second one only start after the 30px of the height of the 1st one.

    — commented 10 months ago by von Goethe

  • hi....von,

    code for you just try it...

    var win = Ti.UI.currentWindow;
     
    var tableview = Ti.UI.createTableView({
        rowHeight : 'auto'
    });
     
    var tblData = [];
     
    for (var i = 0; i < 5; i++) {
     
        var row = Ti.UI.createTableViewRow({
            // height : 'auto'
        });
     
        var lbl1 = Ti.UI.createLabel({
            left : 10,
            width : 150,
            height : 'auto',
            text : 'hello this is babi sarafaraz testing here for the label heights'
        });
     
        var lbl2 = Ti.UI.createLabel({
            left : 170,
            width : 150,
            height : 'auto',
            text : 'Hello I am tested it and it is working fine.'
        });
        if (lbl1.height > lbl2.height) {
            row.height = lbl1.height;
        } else {
            row.height = lbl2.height;
        }
     
        row.add(lbl1);
        row.add(lbl2);
     
        tblData.push(row);
    }
     
    tableview.data = tblData;
     
    win.add(tableview);

    — commented 10 months ago by Sarafaraz Babi

  • TEST CODE IMAGE

    Hey Sarafaraz, I saw the code, thanks, but I don't think I expressed myself correctly, I want both labels to be a single phrase that breaks to a second line if it's too big, example:

    hello this is babi sarafaraz testing here for the label heights Hello I am

    tested it and it is working fine.

    Another Example:

    Victor Santos is shouting: "I love the fighter

    Lyoto Machida, because he is the best and

    I just love Karate".

    I put the spaces between the lines, otherwise, it will show all in a single line.

    — commented 10 months ago by von Goethe

  • Show 1 more comment

Hi

If you want two labels side by side in a single row and the height of the row dependant on the tallest label, then try amending your code as below. This code assumes they both occupy half of the width as in your picture.

I am also going to assume that your rowView is being added to an actual tableViewRow.

var rowView = Ti.UI.createView({
  width:Ti.UI.FILL,
  height:Ti.UI.SIZE,
  top:5,
  bottom:5,
  left:60,
  right:60,
  layout:'horizontal'
})
var rowName = Ti.UI.createLabel({           
  width:'45%',
  height:Ti.UI.SIZE,
  top:0,
  left:0,
  text:response.close.users[i].name+" is shouting ",
  color:'#94A1AF',
  font:{fontSize:14,fontWeight:'normal'}
});
var rowShout = Ti.UI.createLabel({
  width:'45%',
  height:Ti.UI.SIZE,
  top:0,
  left:'5%',
  text:'"'+response.close.users[i].shout+'".',
  color:'#94A1AF',
  font:{fontSize:14,fontWeight:'bold'}
});
 
rowView.add(rowName);
rowView.add(rowShout);
row.add(rowView);
You can obviously change the internal sizing to suit your needs but this will create a parent view that takes up the width of the parent row - with some margin, the adds two labels, each taking up 45%, and the second having a 5% gutter (which you could make 10% of course). The labels will be top aligned inside the view.

See how that works for you.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
8 Comments
  • Thanks Malcolm, but it happened the same with Babi, it broke down in two columns, instead of one continuing phrase.

    SCREENSHOT

    — commented 10 months ago by von Goethe

  • Hi

    I made the code replicate the image you provided indicating what you wanted.

    If this is not correct, can you please try explaining again and providing a mock-up screen-shot, coloring and the labels. As if appears no-one can understand what you are trying to achieve - but we are trying.

    — commented 10 months ago by Malcolm Hollingsworth

  • Hey Malcolm, that screenshot is the result from Babi's code.

    What I wanted is to do a phrase with 2 or more labels, because of the different fonts I want to use, that breaks down into another line, when it is bigger than the current width.

    Below is what I want to achieve, but with 2+ labels.

    Label 1 would be until 'shouting:', label 2 would all after that, see how it breaks down into a new line, and at the beginning, when it doesn't fit in a single line. SCREENSHOT

    — commented 10 months ago by von Goethe

  • Show 5 more comments

Your Answer

Think you can help? Login to answer this question!