文档

Java™教程
隐藏目录
绘制多行文本
导航: 2D 图形
课程: 使用文本 API
部分: 高级文本显示

绘制多行文本

如果您有一段带有样式的文本,想要将其适应特定的宽度,可以使用LineBreakMeasurer类。该类可以将样式文本分成多行,以便其适应特定的视觉进度。每一行都以TextLayout对象的形式返回,该对象表示不可更改的样式字符数据。但是,该类还可以访问布局信息。 TextLayoutgetAscentgetDescent方法返回有关用于在组件中定位行的字体的信息。文本存储为AttributedCharacterIterator对象,以便可以将字体和字号属性与文本一起存储。

以下小程序使用LineBreakMeasurerTextLayoutAttributedCharacterIterator将一段带有样式的文本定位在组件内。


注意:  如果您看不到小程序运行,您需要至少安装Java SE Development Kit (JDK) 7版本。

此小程序的完整代码在LineBreakSample.java中。

以下代码使用字符串vanGogh创建一个迭代器。然后获取迭代器的起始和结束位置,并从迭代器创建一个新的LineBreakMeasurer

    AttributedCharacterIterator paragraph = vanGogh.getIterator();
    paragraphStart = paragraph.getBeginIndex();
    paragraphEnd = paragraph.getEndIndex();
    FontRenderContext frc = g2d.getFontRenderContext();
    lineMeasurer = new LineBreakMeasurer(paragraph, frc);

窗口的大小用于确定应该在哪里断行。此外,对于段落中的每一行,还创建了一个TextLayout对象。

// Set break width to width of Component.
float breakWidth = (float)getSize().width;
float drawPosY = 0;
// Set position to the index of the first
// character in the paragraph.
lineMeasurer.setPosition(paragraphStart);

// Get lines from until the entire paragraph
// has been displayed.
while (lineMeasurer.getPosition() < paragraphEnd) {

    TextLayout layout = lineMeasurer.nextLayout(breakWidth);

    // Compute pen x position. If the paragraph
    // is right-to-left we will align the
    // TextLayouts to the right edge of the panel.
    float drawPosX = layout.isLeftToRight()
        ? 0 : breakWidth - layout.getAdvance();

    // Move y-coordinate by the ascent of the
    // layout.
    drawPosY += layout.getAscent();

    // Draw the TextLayout at (drawPosX,drawPosY).
    layout.draw(g2d, drawPosX, drawPosY);

    // Move y-coordinate in preparation for next
    // layout.
    drawPosY += layout.getDescent() + layout.getLeading();
}

TextLayout类通常不会直接由应用程序创建。然而,当应用程序需要直接处理已应用样式(文本属性)到文本特定位置时,这个类非常有用。例如,在段落中以斜体绘制一个单词,应用程序需要执行测量并为每个子字符串设置字体。如果文本是双向的,这个任务就不那么容易正确完成。从AttributedString对象创建一个TextLayout对象可以帮助你解决这个问题。请参阅Java SE规范获取有关TextLayout的更多信息。


上一页:使用文本属性来样式化文本
下一页:处理双向文本