有没有一种方法可以直接使用样式类自动使用文本


String test = "Hello -this is a string of text";

这样的东西:...每当在ITEXT 7 TextParagraph中使用该文本字符串时,它将使"this" Italic 或某些设置Style

几乎使用特殊字符使Text使用itext7:

在某个Style中出现

我需要这样的东西,因为该程序的用户将需要用某些单词斜体

用户将TextArea输入以定义String。我保存了字符串,然后只制作一个TextParagraph来容纳该字符串:

            Cell location = new Cell()
                    .add(new Paragraph(test);

i虽然使用TextFlow,但由于使用Javafx CSS,这将无法使用ITEXT7。

我想到了这个,它有效,但不是很漂亮...

private Paragraph formatText(String string) {
    Paragraph paragraph = new Paragraph();
        Stream.of(string)
            .map(w -> w.split(" "))
            .flatMap(Arrays::stream)
            .forEach(w -> {
                Text word;
                if (w.startsWith("~")) {
                    String replace = w.replace("~", "");
                    word = new Text(replace);
                    word.setItalic();   
                } else if (w.startsWith("*")) {                 
                    String replace = w.replace("*", "");
                    word = new Text(replace);
                    word.setItalic();       
                } else {                    
                    word = new Text(w);
                }
                paragraph.add(word);                    
            });
    return paragraph;
}

最新更新