Flex 3 - 在形状中对角绘制文本并调整大小

发布于 2024-10-29 07:33:51 字数 790 浏览 7 评论 0原文

我正在尝试创建以下组件:

在此处输入图像描述

仅供参考,空白区域将包含一个文本控件,我正在创建一个组件,该组件代表带有 (i) 图标和“促销”文本的黑色角。
我遇到问题的部分是这个组件代表带有对角线文本的黑色角。文本必须能够容纳 2 行。并且黑角必须能够调整文本的大小。 更重要的是,文本有旋转...

我对如何执行此操作有一些疑问:

  • 我应该为每个文本行设置不同的控件吗?
  • 我应该在形状中绘制文本(旋转后)还是应该重叠两个组件? [当我谈论在形状中绘制文本时,我的意思是与 这个问题]
  • 有没有办法在不做一些奢侈计算的情况下得到三角形的正确大小?

而且......你有什么“更简单”的方法来做到这一点吗?

非常感谢您提供的任何帮助:)我对这个小组件有点迷失:)

问候。
BS_C3


编辑 1:

  • 三角形可以有 2 种尺寸:一种尺寸适合 1 行文本,另一种尺寸适合 2 行文本。
  • 文本将作为单个字符串发送,因此如果需要,它必须自动分为两行
  • 我正在考虑使用图形来绘制三角形,然后使用蒙版来创建圆角 -->这是因为相同的组件将在应用程序的其他地方使用而无需圆角

I'm trying to create the following component:

enter image description here

Just for information, the blank space will contain a text control, and I'm creating a component that represents the black corner with the (i) icon and the "promotion" text.
The part I'm having issues with is this component representing the black corner with the diagonal text. The text has to be able to hold 2 lines. And the black corner has to be able to adjust to the text's size.
What's more, the text has a rotation...

I'm having some doubts on how to do this:

  • Should I have different controls for each text line?
  • Should I draw the text in the shape (after doing the rotation) or should I just overlap both components? [When I talk about drawing the text in the shape, I mean in the same way as asked in this question]
  • Is there any way to get the proper size of the triangle shape without doing some extravagant calculations?

And... do you have any "easier" ways to do this ?

A big thanks for any help you can provide :) I'm a little bit lost with this little component :)

Regards.
BS_C3


Edit 1:

  • The triangle may have 2 sizes: 1 size that will fit 1 line, and another size to fit 2 lines of text.
  • The text will be sent as a single string, so it will have to be automatically divided in two lines if needed
  • I was thinking of using graphics to draw the triangle shape and then use a mask to create the rounded corner --> This is because the same component will be used in other places of the application without the rounded corner

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

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

发布评论

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

评论(2

何时共饮酒 2024-11-05 07:33:51

Flex 非常擅长将组组件更新为任何大小的内容,动态更新。

根据您的建议,我可能会在角落创建“促销”组作为矩形,旋转它并使其适合像你想要的那样,然后使用你用作主要组件背景的圆角矩形的副本,我会制作一个蒙版来剪切“促销”组件,这样你就看不到重叠。

Flex is pretty good at updating group components to whatever size thier contents become, updating dynamically on the fly..

for what your suggesting, I'd probably create the "Promotion" group in the corner as a rectangle, rotate it and fit it to the corner like you want, then using a copy of the rounded rect you use as the maing component background, i'd make a mask to cut the "Promotion" component so you don't see the overlap.

晒暮凉 2024-11-05 07:33:51

好吧,我终于写了这个类,结果是这样的:

public class Corner extends Canvas
{
    private var infoIcon:Image;
    private var text:Text;
    private var triangle:Shape;
    private var bgColor:uint;

    public function Corner()
    {
        super();

        infoIcon = new Image;

        text = new Text;
        text.rotation = -45;
        text.width = 75;
        text.maxHeight = 30;
        text.setStyle('color', '0xffffff');
        text.setStyle('textAlign', 'center');
        text.y = 53;

        this.addChild(infoIcon);
        this.addChild(text);

        triangle = new Shape;
    }

    public function build(bgColor:uint = 0x61113D):void{
        this.bgColor = bgColor;

        // info icon data

        // set text     
        text.addEventListener(FlexEvent.UPDATE_COMPLETE, textHandler);
        text.text = 'blabla';
        text.validateNow();
    }

    /**
     * 
     */
    private function textHandler(e:FlexEvent):void{
        switch(e.type){
            case FlexEvent.UPDATE_COMPLETE:
                text.removeEventListener(FlexEvent.UPDATE_COMPLETE, textHandler);
                drawTriangle();
                break;
        }
    }

    /**
     * 
     */
    private function drawTriangle():void{
        var h:int = 80;
        // check if the text has 1 or 2 lines
        if(text.height > 20)
            h = 95;

        // remove the triangle shape if it exists
        if(this.rawChildren.contains(triangle))
            this.rawChildren.removeChild(triangle);

        // draw triangle
        triangle = new Shape; 
        triangle.graphics.moveTo(0, 0);
        triangle.graphics.beginFill(bgColor);
        triangle.graphics.lineTo(h, 0);
        triangle.graphics.lineTo(0, h);
        triangle.graphics.lineTo(0, 0);
        triangle.graphics.endFill();

        this.rawChildren.addChildAt(triangle,0);

        dispatchEvent(new MyEvent(MyEvent.CORNER_UPDATED));
    }

    ...

}

Well, I finally wrote the class and this is the result:

public class Corner extends Canvas
{
    private var infoIcon:Image;
    private var text:Text;
    private var triangle:Shape;
    private var bgColor:uint;

    public function Corner()
    {
        super();

        infoIcon = new Image;

        text = new Text;
        text.rotation = -45;
        text.width = 75;
        text.maxHeight = 30;
        text.setStyle('color', '0xffffff');
        text.setStyle('textAlign', 'center');
        text.y = 53;

        this.addChild(infoIcon);
        this.addChild(text);

        triangle = new Shape;
    }

    public function build(bgColor:uint = 0x61113D):void{
        this.bgColor = bgColor;

        // info icon data

        // set text     
        text.addEventListener(FlexEvent.UPDATE_COMPLETE, textHandler);
        text.text = 'blabla';
        text.validateNow();
    }

    /**
     * 
     */
    private function textHandler(e:FlexEvent):void{
        switch(e.type){
            case FlexEvent.UPDATE_COMPLETE:
                text.removeEventListener(FlexEvent.UPDATE_COMPLETE, textHandler);
                drawTriangle();
                break;
        }
    }

    /**
     * 
     */
    private function drawTriangle():void{
        var h:int = 80;
        // check if the text has 1 or 2 lines
        if(text.height > 20)
            h = 95;

        // remove the triangle shape if it exists
        if(this.rawChildren.contains(triangle))
            this.rawChildren.removeChild(triangle);

        // draw triangle
        triangle = new Shape; 
        triangle.graphics.moveTo(0, 0);
        triangle.graphics.beginFill(bgColor);
        triangle.graphics.lineTo(h, 0);
        triangle.graphics.lineTo(0, h);
        triangle.graphics.lineTo(0, 0);
        triangle.graphics.endFill();

        this.rawChildren.addChildAt(triangle,0);

        dispatchEvent(new MyEvent(MyEvent.CORNER_UPDATED));
    }

    ...

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