e308. adding an attribute to a string.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 17 行

TXT
17 行
字号
Some applications need to mark a range of characters in a string with an attribute, such as a color. The AttributedString class is a wrapper for a string that provides support for marking ranges of characters with an attribute. An attribute consists of a name, a value, and a contiguous range of characters on which the attribute applies. 
This example marks a word in a string with the attribute called color and the value red. 

    // Declare an attribute name.
    // An attribute name is an object that extends AttributedCharacterIterator.Attribute.
    // Author's note: A more appropriate name would be AttributedCharacterIterator.AttributeName
    static final AttributedCharacterIterator.Attribute COLOR
        = new AttributedCharacterIterator.Attribute("color") {
        };

    // Create the attributed string
    AttributedString astr = new AttributedString("the hot pot");
    
    // Add the COLOR attribute on the word `hot'
    astr.addAttribute(COLOR, "Red", 4, 7);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?