除了删除线外,AttributedString 的任何功能都不起作用
No feature of AttributedString is working other than strikethrough
我必须写一个字符串到 BufferedImage
。我正在使用 AtrributedString
。 TextAttribute.STRIKETHROUGH
正在工作。上标、下标等无效。
public class TextAttributesSuperscript {
static String Background = "input.png";
static int curX = 10;
static int curY = 50;
public static void main(String[] args) throws Exception {
AttributedString attributedString= new AttributedString("this is data. this data should be super script");
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.BOLD, 18), 30,33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 29,33);
attributedString.addAttribute(TextAttribute.SUPERSCRIPT,TextAttribute.SUPERSCRIPT_SUPER,30,33);
final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}
在执行上面的代码时。上标部分不起作用(文本没有像上标一样打印)
我不太确定为什么您的代码不起作用,因为这样做似乎完全合乎逻辑。而且我不明白为什么有些属性有效而有些无效。
但是根据 the Java 2D Tutorial: Using Text Attributes to Style Text,SUPERSCRIPT
属性应该设置在 字体 上,而不是文本本身。 IE。使用 Font.deriveFont(Map<Attribute, ?> attributes)
.
以下对我有用(我稍微修改了你的代码以不依赖于你的背景文件):
public class TextAttributesSuperscript {
static int curX = 10;
static int curY = 50;
public static void main(String[] args) throws Exception {
AttributedString attributedString = new AttributedString("this is data. this data should be super script");
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
Font superScript = new Font("TimesRoman", Font.BOLD, 18)
.deriveFont(Collections.singletonMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER));
attributedString.addAttribute(TextAttribute.FONT, superScript, 30, 33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 30,33);
BufferedImage image = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setColor(Color.WHITE);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}
我必须写一个字符串到 BufferedImage
。我正在使用 AtrributedString
。 TextAttribute.STRIKETHROUGH
正在工作。上标、下标等无效。
public class TextAttributesSuperscript {
static String Background = "input.png";
static int curX = 10;
static int curY = 50;
public static void main(String[] args) throws Exception {
AttributedString attributedString= new AttributedString("this is data. this data should be super script");
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.BOLD, 18), 30,33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 29,33);
attributedString.addAttribute(TextAttribute.SUPERSCRIPT,TextAttribute.SUPERSCRIPT_SUPER,30,33);
final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}
在执行上面的代码时。上标部分不起作用(文本没有像上标一样打印)
我不太确定为什么您的代码不起作用,因为这样做似乎完全合乎逻辑。而且我不明白为什么有些属性有效而有些无效。
但是根据 the Java 2D Tutorial: Using Text Attributes to Style Text,SUPERSCRIPT
属性应该设置在 字体 上,而不是文本本身。 IE。使用 Font.deriveFont(Map<Attribute, ?> attributes)
.
以下对我有用(我稍微修改了你的代码以不依赖于你的背景文件):
public class TextAttributesSuperscript {
static int curX = 10;
static int curY = 50;
public static void main(String[] args) throws Exception {
AttributedString attributedString = new AttributedString("this is data. this data should be super script");
attributedString.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);
Font superScript = new Font("TimesRoman", Font.BOLD, 18)
.deriveFont(Collections.singletonMap(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER));
attributedString.addAttribute(TextAttribute.FONT, superScript, 30, 33);
attributedString.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 30,33);
BufferedImage image = new BufferedImage(400, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setColor(Color.WHITE);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.drawString(attributedString.getIterator(), curX, curY);
g.dispose();
ImageIO.write(image, "png", new File("output.png"));
}
}