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

发布于 2024-11-28 03:57:16 字数 2018 浏览 2 评论 0原文

设置标签内容后如何获取标签的宽度?没有设置初始宽度。

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

label 1 (label 2)

这里最初未设置标签 1 的宽度。随着内容的变化,label 1的宽度发生变化,有时会导致与label 2重叠。

现在我的主要目标是动态设置 label 2 left,使其不与 label 1 重叠。我想这样做:

  1. 首先计算 label 1 的宽度
  2. ,然后设置 label 2 的左侧 = 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();

How can I get the width of a label after set its contents? No initial width set to it.

I have two labels within a table row like followings:

label 1 (label 2)

Here the width of label 1 is not set initially. with the change of contents label 1's width has changes and sometimes it causes overlapping with label 2.

Now my main aim is to set the label 2 left dynamically that it does not ovelapp with label 1. I want to do it like following:

  1. first calculate the width of label 1
  2. then set the left of label 2 = width of label 1 + 10

if there is any other way please help.

HERE SOME CODE

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

森林很绿却致人迷途 2024-12-05 03:57:16
var width = label.toImage().width;

这将获得图像的实际宽度或高度(以像素为单位),并考虑字体属性(大小、系列、粗细)。

var width = label.toImage().width;

This will get the real width or height (in pixels) of the image AND it takes into account the font properties (size, family, weight).

¢好甜 2024-12-05 03:57:16
var labelWidth = label.width;

但要小心“auto”值,因为它们通常返回 0。应该在将标签添加到视图之后请求该值。

[更新]
如果 NumberOfImages.left = name.width+name.left; 不起作用,我会尝试计算宽度,例如

var pixelPerChar = 8;
var pseudoWidth = name.text.length/pixelPerChar;
NumberOfImages.left = pseudoWidth + name.left;
var labelWidth = label.width;

but be careful with 'auto' values since they often return 0. the value should be requested after adding the label to the view.

[update]
if NumberOfImages.left = name.width+name.left; doesn't work i would try to calculate the width like

var pixelPerChar = 8;
var pseudoWidth = name.text.length/pixelPerChar;
NumberOfImages.left = pseudoWidth + name.left;
离不开的别离 2024-12-05 03:57:16

将宽度和高度都设置为 auto,那么它应该返回一个正确的值。

Set both width AND height to auto, then it should return a proper value.

静若繁花 2024-12-05 03:57:16

具有 auto 尺寸的 Titanium 标签往往很烦人(或者是我上次使用 Titanium 时);一个通常有效的解决方案是将它们放置在具有 auto 宽度或高度的 TiView 内部,然后从视图中读取相关属性。

Titanium labels with auto sizes tend to be annoying (or were when I last worked with Titanium); a solution that often works is to place them inside of a TiView with auto width or height and then read the relevant property from the view.

橘和柠 2024-12-05 03:57:16

创建时将 name 的宽度设置为“auto”。

在行中添加name后,可以通过name.size.width获取宽度

Set the width of name to 'auto' when you create it.

After you add name to row, you can get the width by name.size.width

ヤ经典坏疍 2024-12-05 03:57:16

将宽度设置为“自动”似乎会剪辑为 320(如果标签大于屏幕宽度,则标签不会保留一行)。有没有办法让标签比屏幕大并且自动宽度超过屏幕边缘

Setting width to 'auto' seems to clip to 320 (the label won't stay one line if its bigger than the screen width). Is there a way to have a label bigger than the screen with an automatic width go past the screen edg

轻拂→两袖风尘 2024-12-05 03:57:16

我在启动时也遇到此类问题。
你,轻松解决这个问题,你用%age(百分比)设置高度。
这意味着,当你定义
标签高度 = 10%
label.left = 5% 等
然后,它计算屏幕当前高度并以百分比设置默认值。

在这种用途中我们也有利润。当我们改变方向时,它很容易发生,用百分比。

谢谢

I have also this type of problem in Starting.
You, solve this problem easily, u set the height with %age (percentage).
It means, when u define
label.height = 10%
label.left = 5% etc
then, it calculate the screen current height and set by default with percentage.

In This use we have also a profit. when we change the orientation than it take place easily, with percentage.

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文