How to modify letter-spacing in a JTextPane?

半城伤御伤魂 提交于 2019-12-18 08:57:10

问题


I'm need to modify letter-spacing (font tracking) in a JTextPane, and I can't get it to work.

When I'm using a JTextArea, I can just do:

Font font = new Font("Courier New", Font.PLAIN, 10);
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
attrs.put(TextAttribute.TRACKING, -0.1);
font = font.deriveFont(attrs);
textArea.setFont(font);

but as I need to change line spacing, I need to use a JTextPane, and doing:

textPane.setFont(font)

as I did with the JTextArea doesn't work. another thing I tried was:

MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -0.2);
StyleConstants.setFontFamily(set,"Courier New");
StyleConstants.setFontSize(set, 10);
set.addAttribute(TextAttribute.TRACKING, -0.1);
ta.setParagraphAttributes(set, true);

But the tracking attribute doesn't work.

What am I doing wrong?


回答1:


Do you mean kerning? This one shows how to specify custom kerning and some more text effect http://java-sl.com/gp_effects.html



来源:https://stackoverflow.com/questions/5673281/how-to-modify-letter-spacing-in-a-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!