繁体   English   中英

从样式化文档/文本窗格中获取字体字母和颜色

[英]Getting the font letter and color from a Styled Document/Text Pane

最近我一直在使用样式常量、样式文档和文本窗格。 我知道您可以编辑您突出显示/选择的文本的一部分,在下面的代码中显示它

    StyledDocument doc = this.tpText.getStyledDocument();
    Style style = this.tpText.addStyle("stylish", null);
    StyleConstants.setForeground(style, color.BLACK);
    StyleConstants.setFontFamily(style, "Arial");
    doc.setCharacterAttributes(this.tpText.getSelectionStart(), this.tpText.getSelectionEnd() - this.tpText.getSelectionStart(), this.tpText.getStyle("stylish"), true);//This is the piece of code (last line) that will set all the attributes to the highlited text.

例如,如果用户在“hello world”中突出显示/选择“o worl”,则“o worl”将更改为黑色,字体字母将为 Arial。

现在,我的问题是:如何从突出显示/选定的文本中获取字体字母和颜色? 我想知道如何将其保存在单独的变量中(一个用于颜色,另一个用于字体字母)。

好的,经过更多研究,我找到了答案。

doc = this.tpText.getStyledDocument();
    Element element = doc.getCharacterElement(this.tpText.getSelectionStart());
    AttributeSet as = element.getAttributes();
    colour = StyleConstants.getForeground(as);

我看到这个 Style Constant 方法的唯一缺点是它只会识别第一个字符属性并覆盖/忽略其他属性。 也许用循环我可以做到。

暂无
暂无

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

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