简体   繁体   English

Flex自定义按钮组件

[英]Flex custom button component

I want my custom button to look like the button sample below. 我希望我的自定义按钮看起来像下面的按钮示例。 More specifically, I want the width of the button to be determined by the width of the largest word in the label (ie width of "Elongated" in my example). 更具体地说,我希望按钮的宽度由标签中最大单词的宽度(即在我的示例中为“拉长”的宽度)确定。 The label must wrap, not truncate. 标签必须包装,而不是截断。 And I want only one pixel between the top edge of the icon and my button's top edge. 而且我只希望图标的顶部边缘和按钮的顶部边缘之间只有一个像素。 I have tried lots of things but to no avail so far. 到目前为止,我已经尝试了很多方法,但无济于事。 I have reduced the horizontalGap and verticalGap to zero and padding to zero. 我已经将horizo​​ntalGap和verticalGap减小为零,并将padding减小为零。 But still nothing. 但是仍然没有。 Please help. 请帮忙。

Here's what I have right now: 这是我现在所拥有的:

<mx:Button  top="0" cornerRadius="2" paddingLeft="0" paddingRight="0" leading="0" letterSpacing="0" paddingTop="0" paddingBottom="0" verticalGap="0" horizontalGap="0" textAlign="center" labelPlacement="bottom" icon="{MyIcon}" height="60" width="75" label="Elongated Label" fontSize="10" />

替代文字

That's not at all simple. 这一点都不简单。

You will need to create your own button, 您将需要创建自己的按钮,

public class Mybutton extends Button {...}

override createChildren and set the word wrap of the IUITextField used for the label to true. 覆盖createChildren并将用于标签的IUITextField的自动换行设置为true。

override measure and use your own line metrics to determine the width that the button should be. 覆盖度量并使用您自己的线度量来确定按钮应该的宽度。 If you get the measure right, the button will lay itself out properly. 如果您采取正确的措施,该按钮将正确布局。

I don't have a dev environment in front of me at the moment, but something along these lines should work: 我目前没有开发环境,但是应该遵循以下原则:

  1. Set truncateToFit property of the Label to false (OR use a TextField with wordWrap set to true - I think this should keep the words together as much as possible). 将Label的truncateToFit属性设置为false(或使用将wordWrap设置为true的TextField-我认为这应将单词尽可能地保持在一起)。

I haven't had the need to use any of the above yet, and hopefully you haven't tried them yet because it would be an easy solution to a part of your problem. 我还不需要使用以上任何一种方法,希望您还没有尝试过使用它们,因为这将是解决部分问题的简便方法。 Without any code, I'm not sure why padding didn't work, but maybe it's something to do with the word truncation. 没有任何代码,我不确定为什么填充不起作用,但可能与截断一词有关。

As an alternative, why not use a Button , embed or specify a source for your icon and decide where to place the text by specifying the object's labelPlacement property? 或者,为什么不使用Button ,为图标嵌入或指定源,并通过指定对象的labelPlacement属性来决定将文本放置在何处?

EDIT: Since there's no property in Button about wordWrap, as they would recommend in the Adobe Flex forums for such questions regarding sizing based on content where there is no automatic feature to do that, you have to find the longest word and adjust the width of the button (ie: in the creationComplete event). 编辑:由于Button中没有关于wordWrap的属性,正如他们在Adobe Flex论坛中建议的那样,针对基于没有自动功能的内容进行大小调整的此类问题,您必须找到最长的单词并调整宽度按钮(即:creationComplete事件中)。 Experimenting to find the font to pixel ratio would be my best bet (or you can use a Monospace font where all the characters are given the same space thereby simplifying the estimation): 尝试找到字体与像素的比率是我最好的选择(或者您可以使用等宽字体,其中所有字符都被赋予相同的空格,从而简化了估算):

creationComplete="event.target.width=returnMyWidth();"

As for the padding, it may be related to the custom width that you had set or it may be from embedding the image automatically setting a padding by being included - I'm not really sure, so it would be good if someone can offer a comment based on experience with this. 至于填充,它可能与您设置的自定义宽度有关,也可能是由于嵌入图像而被自动包含在内而设置的-我不确定,因此,如果有人可以提供根据此经验发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM