0

我正在尝试将动态生成的文本放在贝塞尔曲线上 - 文本最长可达 12 个字符,因此需要调整到中心。

我在谷歌上找不到任何尝试这样做。

我唯一的猜测是读取贝塞尔曲线 xy,然后将文本字段放在上面,但我如何让文本字段适合曲线?

谢谢!

马库斯

4

1 回答 1

4

如果你的曲线是圆弧或圆,我有适合你的东西:p 前段时间我做了两个类来处理这个问题,它们来了:

    package com.display{

    import flash.display.Sprite;

    import utils.Utils;

    public class PathText extends Sprite
    {
        private var _text:String;
        private var _chars:Array;
        private var _startAngle:Number;
        private var _stopAngle:Number;
        private var _radius:Number;


        public function PathText(__text:String, __radius:Number)
        {
            super();

            this.mouseChildren = false;
            this.mouseEnabled = false;
            _text = __text;

            _startAngle = 0;
            _radius = __radius;
            _chars = new Array();

            init();

        }

        private function init():void
        {
            for(var i:int = 0; i < _text.length; i++)
            {
                var char:Char = new Char(_text.charAt(i));
                _chars.push(char);
            }
            drawArc(_radius);
        }

        public function drawArc(rad:Number):void
        {
            var lastAngle:Number = 0;
            for(var i:int = 0; i < _chars.length; i++)
            {
                var angle:Number = 2 * Math.sin((_chars[i].width/2)/rad);

                _chars[i].rotation = radiansToDegrees(lastAngle) + 90;
                _chars[i].x = rad * Math.cos(lastAngle);
                _chars[i].y = rad * Math.sin(lastAngle);
                lastAngle += angle;
                addChild(_chars[i]);
            }

            _stopAngle = radiansToDegrees(lastAngle);
        }

        public function destroy():void
        {
            for(var i:int = _chars.length; i >= 0; i--) 
            {
                removeChild(_chars[i]);
                _chars.pop().destroy();
            }
        }

        public function get startAngle():Number
        {
            return _startAngle;
        }

        public function set startAngle(value:Number):void
        {
            _startAngle = value;
        }

        public function get stopAngle():Number
        {
            return _stopAngle;
        }

        public function set stopAngle(value:Number):void
        {
            _stopAngle = value;
        }

        private function radiansToDegrees(radians:Number):Number
        {
            return radians * 180 / Math.PI;
        }

    }
}

和 Char 类:

package com.display
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;

    public class Char extends Sprite
    {
        private var _str:String;

        public function Char(__str:String)
        {
            super();

            _str = __str;
            this.mouseChildren = false;
            this.mouseEnabled = false;
            init();
        }

        private function init():void
        {
            var _tf:TextField       = new TextField();
            _tf.defaultTextFormat   = new TextFormat("Some Font here", 15, 0xffffff);
            _tf.embedFonts          = true;
            _tf.mouseEnabled        = false;
            _tf.selectable          = false;
            _tf.text                = _str;
            _tf.autoSize            = TextFieldAutoSize.LEFT;
            _tf.width               = _tf.textWidth;
            addChild(_tf);
        }

        public function destroy():void
        {
            this.removeChildAt(0);
        }

        public function get str():String
        {
            return _str;
        }

        public function set str(value:String):void
        {
            _str = value;
        }

    }
}

告诉我它是否适合你……干杯!

于 2012-07-11T13:45:53.907 回答