设置spark.components.Label的文本颜色
我有这个非常简单的自定义组件,由两个 标签:_left 和 _right。
它应该代表游戏中的几种得分: 1.2.3.4.5.6
最后一个数字(上例中的“6”)如果刚刚改变(在当前游戏回合),否则所有数字应该看起来相同。
最后一个数字也应该是红色或绿色(取决于它是“坏”还是“好”分数)。
这是我的 ScoreLabel.mxml 源代码:
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
gap="0">
<fx:Script>
<![CDATA[
public function setText(str:String, changed:Boolean=false):void {
if (!changed) {
_right.text = '';
_left.text = str;
return;
}
var array:Array = str.split('.');
_right.text = array.pop();
_left.text = array.join('.') + '.';
}
public function setColor(n:uint):void {
_right.setStyle('color', n);
trace('setColor: ' + n);
}
]]>
</fx:Script>
<s:Label id="_left" width="100%" textAlign="right"/>
<s:Label id="_right" width="25" fontWeight="bold" color="#006600"/>
</s:HGroup>
我的问题是:当我调用 myLabel.setColor(0xFF0000); 时,文本不会更改为红色,而是保持默认的绿色 - 即使我可以在调试器中看到痕迹。
请问有什么想法,为什么颜色没有改变?
我还知道,我可以将上述组件更改为:
private var _color:uint;
<s:Label id="_right" width="25" fontWeight="bold" color="{_color}"/>
并更改 _color 成员,但我不想再引入一个数据绑定,因为我的中将有许多 ScoreLabel application:
(如上所示,所有数字都是绿色 - 尽管 setColor(0xFF0000)有被叫了3次)。
I have this very simple custom component, made out of two Labels: _left and _right.
It should represent several kinds of score in a game: 1.2.3.4.5.6
The last number ("6" in the above example) should be in bold font if it has just changed (in the current game round), otherwise all numbers should look same.
Also the last number should be red or green (depending if it's "bad" or "good" score).
Here is my source code for ScoreLabel.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
gap="0">
<fx:Script>
<![CDATA[
public function setText(str:String, changed:Boolean=false):void {
if (!changed) {
_right.text = '';
_left.text = str;
return;
}
var array:Array = str.split('.');
_right.text = array.pop();
_left.text = array.join('.') + '.';
}
public function setColor(n:uint):void {
_right.setStyle('color', n);
trace('setColor: ' + n);
}
]]>
</fx:Script>
<s:Label id="_left" width="100%" textAlign="right"/>
<s:Label id="_right" width="25" fontWeight="bold" color="#006600"/>
</s:HGroup>
My problem is: when I call myLabel.setColor(0xFF0000); the text doesn't change to red, but stays in the default green color - even though I can see the traces in debugger.
Any ideas please, why the color doesn't change?
Also I know, that I could change the above component to:
private var _color:uint;
<s:Label id="_right" width="25" fontWeight="bold" color="{_color}"/>
and change that _color member, but I'd prefer not do introduce one more data binding, because I'm going to have many ScoreLabel's in my application:
(as you see above, all numbers are green - eventhough setColor(0xFF0000) has been called 3 times).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎如果没有隐式转换为 Label,Flash Builder Premium 4.6 根本无法编译,因为它说了以下内容:
如果您将:更改
为:
它应该有效。
It seems that without an implicit cast to Label, Flash Builder Premium 4.6 simply won't compile, since it says the following:
If you change:
to this:
it should work.