在Titanium移动应用程序中获取标签的宽度

the*_*dox 1 iphone titanium titanium-mobile

设置好内容后如何获得标签的宽度?没有设置初始宽度.

我在表格行中有两个标签,如下所示:

label 1 (label 2)
Run Code Online (Sandbox Code Playgroud)

这里的宽度label 1最初没有设置.随着内容label 1宽度的变化有变化,有时会导致重叠label 2.

现在我的主要目标是label 2动态地设置左边,它不会过度使用label 1.我想这样做如下:

  1. 首先计算宽度 label 1
  2. 然后设置左边 label 2 = width of label 1 + 10

如果有任何其他方式请帮助.

这里有一些代码

var win = Titanium.UI.createWindow({
    title: 'Cleos',
    backgroundImage: 'tablebg.png',
    navBarHidden: true,
});
var tableview = Titanium.UI.createTableView({
    backgroundColor: 'transparent',
    separatorStyle: Ti.UI.iPhone.TableViewSeparatorStyle.NONE,
    style: Titanium.UI.iPhone.TableViewStyle.PLAIN
});
win.add(tableview);
var modelData = new Array;
for (i = 0; i <= daga.length - 1; i++) { // `data` as json, comes from a source
    var row = Titanium.UI.createTableViewRow({
        height: 120.0,
        backgroundColor: 'transparent',
        selectedBackgroundColor: '#380710',
        hasDetail: true
    });
    var imageThumb = Titanium.UI.createImageView({
        image: '...',
        width: 100.0,
        height: 100.0,
        left: 4.0,
        top: 10.0
    });
    row.add(imageThumb);
    var name = Titanium.UI.createLabel({
        text: '...',
        font: {
            fontSize: 16,
            fontWeight: 'bold'
        },
        textAlign: 'left',
        color: '#fff',
        height: 30.0,
        top: 5.0,
        left: 110.0
    });
    row.add(modelname);
    var NumberOfImages = Titanium.UI.createLabel({
        text: '(' + 12 + ')',
        font: {
            fontSize: 16
        },
        width: 'auto',
        color: '#bfbebe',
        textAlign: 'left',
        height: 30.0,
        top: 5.0,
        left: '' // I want to set this `left  = width of label name`
    });
    row.add(NumberOfImages);
    modelData.push(row);
    tableview.setData(modelData);
}
win.open();
Run Code Online (Sandbox Code Playgroud)

Bog*_*sca 6

var width = label.toImage().width;
Run Code Online (Sandbox Code Playgroud)

这将获得图像的实际宽度或高度(以像素为单位),并考虑字体属性(大小,族,重量).