0
如果我在SWT上寫一個文本以特定的字體/樣式說「這個文本」,我想要't''h''i' 's'等等。有什麼辦法可以獲得同樣的結果嗎?在SWT文本部件中獲取單個字符的x,y座標
如果我在SWT上寫一個文本以特定的字體/樣式說「這個文本」,我想要't''h''i' 's'等等。有什麼辦法可以獲得同樣的結果嗎?在SWT文本部件中獲取單個字符的x,y座標
Text text = new Text(parent, SWT.NONE);
text.setText("This");
GC gc = new GC(text);
int currentPixelsX = text.getBounds().x;
int currentPixelsY = text.getBounds().y;
for (int i = 0; i < text.getText().length(); i++) {
System.out.println("X co-ordinate of " + text.getText().charAt(i) + ":" + currentPixelsX);
System.out.println("Y co-ordinate of " + text.getText().charAt(i) + ":" + currentPixelsY);
currentPixelsX = currentPixelsX + gc.getCharWidth(text.getText().charAt(i));
}
這給出了相對於文本框的父親的像素座標。
大概this片段可以幫你計算字符
你的建議無法容納的GDI的字符間距/字間距調整該解決方案的各個長度。 – 2009-10-11 15:54:03