0

我有一个在BitmapData. 问题是,TextFormat指定不影响图像上的文字。我在文本格式中指定的大小、字体根本没有使用。

function drawString(target:BitmapData,text:String,color:uint,x:Number,y:Number):void {
        var channelName:TextField = new TextField();

        channelName.textColor=color;
        channelName.antiAliasType = AntiAliasType.ADVANCED;
        channelName.alpha=1.0;

        var txtFormat:TextFormat = new TextFormat("Verdana",25,color,true);
        txtFormat.size=Number(25);
        txtFormat.font="Verdana";
        txtFormat.bold=true;
        channelName.setTextFormat(txtFormat);
        channelName.text = text;

        channelName.defaultTextFormat = txtFormat;
        channelName.cacheAsBitmap = true;
        channelName.width=400;

        var mat:Matrix = new Matrix();
        mat.translate(x,y);
        target.draw(channelName,mat);

    }

如何自定义在 ? 上绘制的文本字体和大小BitmapData

4

2 回答 2

1

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" width="640" height="400" creationComplete="init(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        [Embed(source='com/drawtext/Image.jpg')]
        public var Picture:Class;

        protected function init(event:FlexEvent):void
        {
            var bitmapData:BitmapData = new BitmapData(640, 400, true, 0xFF82DC);
            bitmapData.draw(new Picture());
            img.source = bitmapData;
            drawString(bitmapData, "It is Yours!", 0xff0000, 70, 60);
        }

        protected function drawString(target:BitmapData,text:String,color:uint,x:Number,y:Number):void 
        {
            var channelName:TextField = new TextField();

            channelName.antiAliasType = AntiAliasType.ADVANCED;

            var txtFormat:TextFormat = new TextFormat();
            txtFormat.size = 35;
            txtFormat.font = "Verdana";
            txtFormat.bold = true;

            channelName.defaultTextFormat = txtFormat;

            channelName.width = 400;
            channelName.height = 40;
            channelName.textColor=color;
            channelName.text = text;

            var mat:Matrix = new Matrix();
            mat.translate(x, y);

            target.draw(channelName, mat);
        }

    ]]>
</fx:Script>

<s:Image id="img" width="640" height="400"/>
</s:WindowedApplication>

结果:

在此处输入图像描述

于 2013-04-10T16:51:19.803 回答
0

在设置文本格式之前尝试设置 text 属性 - 并使用 setTextFormat(...) 或 defaultTextFormat = ... 不是两者

您也可以删除这些行:

//does the same thing as new TextFormat("Verdana",25,color,true)
txtFormat.size=Number(25);
txtFormat.font="Verdana";
txtFormat.bold=true;

//only useful if you add textField to the display list (ie: if you did addChild(channelName)
channelName.cacheAsBitmap = true;
于 2013-04-10T18:39:28.823 回答