[英]java.awt.font letter spacing
使用 java.awt.font 時是否可以增加字母間距? 像這樣:
Font font = new Font("serif", Font.PLAIN, 21);
//Increase the spacing - perhaps with deriveFont()?
BufferedImage image = new BufferedImage(400, 600, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setFont(font);
graphics.drawString("testing",0,0);
您可以派生一種新字體,具有更新的跟蹤屬性:
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
attributes.put(TextAttribute.TRACKING, 0.5);
Font font2 = font.deriveFont(attributes);
gr.setFont(font2);
gr.drawString("testing",0,20);
通用的“將此文本置於寬度為 w 的容器內的當前字體中心”可能是:
/**
* Calculates the x-coordinate to use to center the specified message
* horizontally during a subsequent call to g.drawString().
*
* @param g - the instance of the Graphics object to use.
* @param message - the string containing the message to be centered.
* @param width - the width of the container to center the message in.
* @return - the integer x-coordinate to use in a subsequent call to
* g.drawString()
*/
private int centerMessageHorizontal(Graphics g, String message, int width)
{
FontMetrics fontMetrics = g.getFontMetrics();
int textWidth = fontMetrics.stringWidth(message);
return width / 2 - textWidth / 2;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.