我正在使用java來繪製一些文本,但我很難計算字符串的寬度。 例如: zheng中國... 這個字符串佔用多長時間?如何計算字體寬度?
回答
對於單個字符串,您可以獲取給定圖形字體的度量標準,並使用它來計算字符串大小。例如:
String message = new String("Hello, StackOverflow!");
Font defaultFont = new Font("Helvetica", Font.PLAIN, 12);
FontMetrics fontMetrics = new FontMetrics(defaultFont);
//...
int width = fontMetrics.stringWidth(message);
如果具有更復雜的文本佈局要求,如給定寬度內流動的一段文字,則可以創建一個java.awt.font.TextLayout
對象,如本實施例中(從文檔):
Graphics2D g = ...;
Point2D loc = ...;
Font font = Font.getFont("Helvetica-bold-italic");
FontRenderContext frc = g.getFontRenderContext();
TextLayout layout = new TextLayout("This is a string", font, frc);
layout.draw(g, (float)loc.getX(), (float)loc.getY());
Rectangle2D bounds = layout.getBounds();
bounds.setRect(bounds.getX()+loc.getX(),
bounds.getY()+loc.getY(),
bounds.getWidth(),
bounds.getHeight());
g.draw(bounds);
Can not instan指定FontMetrics類型。 – 2011-10-19 08:47:07
使用:FontMetrics fontMetrics = g.getFontMetrics(); – 2011-10-20 17:37:17
java.awt.FontMetrics是抽象的;不能實例化 FontMetrics metrics = new FontMetrics(font); 我在其他地方發現你必須在(字體)和分號之前放置一對空的大括號。我不知道爲什麼。 FontMetrics指標=新的FontMetrics(字體){}; – 2013-08-26 16:40:58
這裏是一個簡單的應用程序,可以顯示你測試字符串的寬度時,如何使用的FontMetrics:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUITest {
JFrame frame;
public static void main(String[] args){
new GUITest();
}
public GUITest() {
frame = new JFrame("test");
frame.setSize(300,300);
addStuffToFrame();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
frame.setVisible(true);
}
});
}
private void addStuffToFrame() {
JPanel panel = new JPanel(new GridLayout(3,1));
final JLabel label = new JLabel();
final JTextField tf = new JTextField();
JButton b = new JButton("calc sting width");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FontMetrics fm = label.getFontMetrics(label.getFont());
String text = tf.getText();
int textWidth = fm.stringWidth(text);
label.setText("text width for \""+text+"\": " +textWidth);
}
});
panel.add(label);
panel.add(tf);
panel.add(b);
frame.setContentPane(panel);
}
}
在這個偉大的演示看看,特別是「文字測量」部分。它解釋了可用的尺寸和用途:Advanced Java 2D™ topics for Desktop Applications。
在下面的類中Java2D FAQ: What is the difference between logical, visual and pixel bounds?
鏈接到「桌面應用程序的高級Java 2D™主題」已經死了。 – dastrobu 2015-10-12 09:01:51
使用方法的getWidth更多的一些信息:
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
class StringMetrics {
Font font;
FontRenderContext context;
public StringMetrics(Graphics2D g2) {
font = g2.getFont();
context = g2.getFontRenderContext();
}
Rectangle2D getBounds(String message) {
return font.getStringBounds(message, context);
}
double getWidth(String message) {
Rectangle2D bounds = getBounds(message);
return bounds.getWidth();
}
double getHeight(String message) {
Rectangle2D bounds = getBounds(message);
return bounds.getHeight();
}
}
您可以從Font.getStringBounds找到它()的
String string = "Hello World";
// Passing or initializing an instance of Font.
Font font = ...;
int width = (int) font.getStringBounds(string, new FontRenderContext(font.getTransform(), false, false)).getBounds().getWidth();
請解釋你的解決方案 – Jens 2015-04-02 05:48:39
- 1. 如何計算多字符串的字體寬度和高度?
- 2. PHP GD字體寬度計算幫助
- 3. 根據可用的寬度和字體計算文本高度?
- 4. 如何以像素爲單位計算字符串字體寬度?
- 5. 計算器高度寬度
- 6. 如何計算最佳titleView寬度?
- 7. 如何計算的列表項寬度
- 8. jquery/javascript如何快速計算寬度
- 9. 如何計算grandchildren div的寬度?
- 10. 字體寬度
- 11. 如何設置容器寬度計算其子女的寬度
- 12. 如何從CFont對象中獲取自動計算的字體寬度?
- 13. 我們如何計算任何文字的寬度
- 14. css計算函數:如何計算文本的寬度?
- 15. 計算Spark TextArea寬度
- 16. IE計算寬度問題
- 17. 計算寬度與jQuery
- 18. 計算與自動寬度
- 19. 如何計算iText中的字符串寬度?
- 20. 如何根據字段大小計算dbEdit的寬度?
- 21. 如何計算Ruby中的字符串寬度?
- 22. 如何計算px中字符串的寬度?
- 23. 如何計算適當的字段寬度?
- 24. Gtk如何計算像素的字符串寬度
- 25. 如何僅在iOS中使用相機計算物體的高度和寬度?
- 26. 如何找到字體的寬度?
- 27. 如何計算div中最寬單詞的寬度?
- 28. 造型進度條 - 計算寬度
- 29. 計算UIBezierPath寬度和高度?
- 30. 如何製作具有計算寬度的進度條
可能的複製[在Java中計算字符串的顯示寬度](http://stackoverflow.com/questions/258486/calculate-the-display-width-of-a-string-in-java) – Zich 2017-04-09 13:11:41