【发布时间】:2012-08-03 15:56:50
【问题描述】:
我有一个JComponent,它使用Textlayout 绘制一个多行属性字符串,运行得非常好:
public static float drawMultiAttributedString(Graphics2D g2d, AttributedString str, String plainString, int maxWidth, int startX, float startY){
FontRenderContext fontRenderCtx = g2d.getFontRenderContext();
AttributedCharacterIterator attrCharIter = str.getIterator();
LineBreakMeasurer lineBreakMeasurer = new LineBreakMeasurer(attrCharIter, fontRenderCtx);
float x = startX;
float y = startY;
int next;
int limit; //länge bis zum nächsten umbruch
int charat;
String tested = plainString;
while (lineBreakMeasurer.getPosition() < attrCharIter.getEndIndex()) {
next = lineBreakMeasurer.nextOffset(maxWidth);
limit = next;
charat = tested.indexOf("\n", lineBreakMeasurer.getPosition()+1);
if(next > (charat - lineBreakMeasurer.getPosition()) && charat != -1){
limit = charat - lineBreakMeasurer.getPosition();
}
TextLayout layout = lineBreakMeasurer.nextLayout(maxWidth, lineBreakMeasurer.getPosition() + limit, false);
y += layout.getAscent(); //höhe des haupttextes
layout.draw(g2d, x, y);
y += layout.getDescent() + layout.getLeading();
}
return y - startY;
}
问题是,Textlayout 不呈现我需要的制表符。
有谁知道我如何包括制表符?有什么想法吗?
非常感谢!
【问题讨论】:
标签: java swing text line-breaks tabular