WordEditor2 中编辑器的初期字体被配置到 config 目录下的配置文件中,默认为“宋体”文字大小默认为“14”。系统初期化时从配置文件中读取并以 Style 的方式设置为编辑器的默认值。WordEditor2 的编辑器是继承自 JTextPane ,所以它拥有解析 html 代码的能力。为了能够设置编辑器的全局字体,我在这里的处理是为编辑器追加 body 的 css 配置。
StringBuffer style = new StringBuffer();
style.append("body {");
style.append("font-size:");
style.append(wep.get("editor.font.size"));
style.append(";font-family:");
style.append(wep.get("editor.font.family"));
style.append(";margin:");
style.append(wep.get("editor.margin"));
style.append(";background:");
style.append(wep.get("editor.background"));
style.append(";");
this.doc.getStyleSheet().addRule(style.toString());
按照以上方法设置后,编辑器的 html 代码中就会生成 body 的 css 配置。这样一来,就达到了我们改变编辑器全局字体的目的。
body { margin-right: 10px; font-family: 宋体; background-attachment: scroll; margin-bottom: 15px; background-color: #CCC8C8; margin-top: 5px; margin-left: 10px; font-size: 14; background-repeat: repeat }
其实默认字体就挺好的。
这个功能主要是为了实现编辑器的全局字体变换。
学习一下~~