获取 Titanium 移动应用程序中标签的宽度
设置标签内容后如何获取标签的宽度?没有设置初始宽度。
我在表行中有两个标签,如下所示:
label 1 (label 2)
这里最初未设置标签 1 的宽度。随着内容的变化,label 1
的宽度发生变化,有时会导致与label 2
重叠。
现在我的主要目标是动态设置 label 2
left,使其不与 label 1
重叠。我想这样做:
- 首先计算
label 1
的宽度 - ,然后设置
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:
- first calculate the width of
label 1
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这将获得图像的实际宽度或高度(以像素为单位),并考虑字体属性(大小、系列、粗细)。
This will get the real width or height (in pixels) of the image AND it takes into account the font properties (size, family, weight).
但要小心“auto”值,因为它们通常返回 0。应该在将标签添加到视图之后请求该值。
[更新]
如果
NumberOfImages.left = name.width+name.left;
不起作用,我会尝试计算宽度,例如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将宽度和高度都设置为 auto,那么它应该返回一个正确的值。
Set both width AND height to auto, then it should return a proper value.
具有
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 aTiView
withauto
width or height and then read the relevant property from the view.创建时将
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 byname.size.width
将宽度设置为“自动”似乎会剪辑为 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
我在启动时也遇到此类问题。
你,轻松解决这个问题,你用%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