3

我需要在 Flex 中模拟一些旧的 DOS 或大型机终端。例如,如下图所示。

替代文字

不同颜色的文本很容易,但是制作不同背景颜色的能力,例如黄色背景,超出了标准 Flash 文本的能力。

我可能还需要能够在某些地方输入文本并将文本向上滚动到“终端”。知道我会如何攻击这个吗?或者更好的是,这种事情的任何现有代码/组件?

4

2 回答 2

2

用于TextField.getCharBoundaries在需要背景的区域中获取第一个和最后一个字符的矩形。从这些矩形中,您可以构建一个跨越整个区域的矩形。使用它在Shape文本字段后面或文本字段的父级中绘制背景。

更新您要求的示例,以下是如何从一系列字符中获取矩形:

var firstCharBounds : Rectangle = textField.getCharBoundaries(firstCharIndex);
var lastCharBounds  : Rectangle = textField.getCharBoundaries(lastCharIndex);

var rangeBounds : Rectangle = new Rectangle();

rangeBounds.topLeft = firstCharBounds.topLeft;
rangeBounds.bottomRight = lastCharBounds.bottomRight;

如果你想为整条线找到一个矩形,你可以这样做:

var charBounds : Rectangle = textField.getCharBoundaries(textField.getLineOffset(lineNumber));

var lineBounds : Rectangle = new Rectangle(0, charBounds.y, textField.width, firstCharBounds.height);

当您拥有要为其绘制背景的文本范围的边界时,您可以在updateDisplayList文本字段的父级方法中执行此操作(假设文本字段位于 [0, 0] 并且具有白色文本,并且这textRangesWithYellowBackground是一个矩形数组,代表应该有黄色背景的文本范围):

graphics.clear();

// this draws the black background
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, textField.width, textField.height);
graphics.endFill();

// this draws yellow text backgrounds
for each ( var r : Rectangle in textRangesWithYellowBackground )
    graphics.beginFill(0xFFFF00);
    graphics.drawRect(r.x, r.y, r.width, r.height);
    graphics.endFill();
}
于 2008-09-13T15:05:16.237 回答
1

字体的宽度和高度是固定的,因此动态制作背景位图并不难,而且可能是最快捷、最简单的解决方案。事实上,如果你正确调整它的大小,每个字符只会有一个拉伸像素。

根据字符的背景为像素(或多个像素)着色。

-亚当

于 2008-09-13T14:20:47.293 回答