我有一个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
不会呈现我需要的制表符。有谁知道我如何包括制表符?有任何想法吗?
非常感谢!