简体   繁体   English

在SWT文本小部件中获取单个字符的x,y坐标

[英]Obtain x,y coords of individual Chars in a SWT text widget

If I write a text on SWT say "this text" in a particular font/style , I want the individual coords of 't' 'h' 'i' 's' and so on. 如果我在SWT上写了某种特定字体/样式的文本“ this text”,我希望使用“ t”,“ h”,“ i”,“ s”等单个坐标。 Is there any way to obtain the same? 有没有办法获得相同的?

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 gives the the pixel co-ordinates with respect to the parent of the text box. 这给出了相对于文本框父级的像素坐标。

代码段可能可以帮助您计算字符的各个长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM