在ExtJS中,是否可以使文本字段的标签的大小最佳以适合一行文本?
该labelWidth
属性没有自动设置,将其完全保留与指定默认值100效果相同,这将导致冗长的文本跨越多行:
我希望标签的宽度与不包含换行符就包含整个文本所需的宽度一样,并且可编辑字段填充可用宽度的其余部分。
您还可以Ext.util.TextMetrics
用来测量标签的长度并进行labelWidth
相应设置。(请参阅此小提琴)。
var label = 'Changing text with variable length',
tm = new Ext.util.TextMetrics(),
n = tm.getWidth(label + ":"); //make sure we account for the field separator
Ext.create('Ext.form.Panel', {
bodyPadding: 10,
bodyStyle: 'background-color: #cef',
items: [{
xtype: 'textfield',
emptyText: 'no data',
fieldLabel: label,
labelWidth: n
}],
margin: 25,
renderTo: Ext.getBody()
});
如果需要更通用的解决方案,请看一下Extlem docs中slemmon提供的注释中的示例。本质上,他的示例创建了文本字段的扩展,该扩展基于标签动态设置标签宽度。这是他的例子:
Ext.define('MyTextfield', {
extend: 'Ext.form.field.Text',
xtype: 'mytextfield',
initComponent: function () {
var me = this,
tm = new Ext.util.TextMetrics();
me.labelWidth = tm.getWidth(me.fieldLabel) + 10;
tm.destroy();
me.callParent(arguments);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句