简体   繁体   English

将多个StyleConstant添加到AttributeSet

[英]Adding multiple StyleConstants to an AttributeSet

I have a string of text: "This is a |<good>| mountain to |<ski>| on." 我有一串文字:“这是在| <ski> |上的| <good> |山。”

I want |<good>| 我要| <good> | and |<ski>| | <ski> | to appear red, italic, FontSize 9. 显示为红色,斜体,FontSize 9。

I've set individual AttributeSets 我已经设置了单独的AttributeSet

  StyleContext myProtected = StyleContext.getDefaultStyleContext();
  AttributeSet attR = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Foreground, Color.RED);
  AttributeSet attB = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Background, Color.BLUE);
  AttributeSet attI = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Italic, Boolean.TRUE);
  AttributeSet attS = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.FontSize, 9);

and I have a regex that finds the patterns correctly. 我有一个正则表达式可以正确找到模式。 But if i try to set multiple AttributeSets to the same match, only the first one respects the regex. 但是,如果我尝试将多个AttributeSet设置为相同的匹配项,则只有第一个尊重正则表达式。 The others just apply themselves to the whole string. 其他人只是将自己应用于整个字符串。 Here's the whole class: 这是整个班级:

class ElementHighlight2 extends DefaultStyledDocument {
  //private final  MutableAttributeSet XRED = new SimpleAttributeSet();

  StyleContext myProtected = StyleContext.getDefaultStyleContext();
  AttributeSet attR = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Foreground, Color.RED);
  //AttributeSet attB = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Background, Color.BLUE);
  AttributeSet attI = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Italic, Boolean.TRUE);
  AttributeSet attS = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.FontSize, 9);    

@Override
  public void insertString (int offset, String Pstr, AttributeSet RedBlue) throws BadLocationException
  {
   super.insertString(offset, Pstr, RedBlue);
   String text = getText(0, getLength());
   int pre = pipeCharEnd(text, offset);
   if (pre < 0) pre = 0;
   int post = pipeCharStart(text, offset + Pstr.length());
   int prewords = pre;
   int postwords = pre;

   while (postwords <= post) {
                if (postwords == post || String.valueOf(text.charAt(postwords)).matches("\\|")) {
                    if (text.substring(prewords, postwords).matches("(\\|\\<[^\\>]*\\>)"))
                        setCharacterAttributes(prewords, postwords - prewords +1, attR, false);
                        setCharacterAttributes(prewords, postwords - prewords +1, attI, false);
                        setCharacterAttributes(prewords, postwords - prewords +1, attS, false);

                    prewords = postwords;
                }
                postwords++;
            }
  }

If somebody could help me learn the best practice that I have not yet discovered to achieve this, I will be most grateful. 如果有人可以帮助我学习尚未实现的最佳实践,我将不胜感激。

Instead of creating individual attributes and combining them with setCharacterAttributes , I think you could create a combined attribute set: 我认为您可以创建一个组合的属性集,而不是创建单个属性并将其与setCharacterAttributes组合起来:

AttributeSet attR = myProtected.addAttribute(myProtected.getEmptySet(), StyleConstants.Foreground, Color.RED);
AttributeSet attI = myProtected.addAttribute(attR, StyleConstants.Italic, Boolean.TRUE);
AttributeSet attS = myProtected.addAttribute(attI, StyleConstants.FontSize, 9);    

and then apply only the combined attS . 然后仅应用组合的attS

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

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