Font settings for strings in java

后端 未结 4 1699
轮回少年
轮回少年 2020-12-10 21:44

Can I set font properties for a string in java.

相关标签:
4条回答
  • 2020-12-10 22:00

    A String doesn't have a font, as it's completely separate from any way to display it. Fonts are related to the user interface components you're using to present the String to your users, how to set it would depend on those user interface components.

    0 讨论(0)
  • 2020-12-10 22:00

    You can use AttributedString. Here is some examples: JavaDocExamples

    Font font = new Font("LucidaSans", Font.PLAIN, 14);
    AttributedString messageAS = new AttributedString(textMessage);
    ...
    messageAS.addAttribute(TextAttribute.FONT, font);
    AttributedCharacterIterator messageIterator = messageAS.getIterator();
    FontRenderContext messageFRC = graphics2D.getFontRenderContext();
    LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
    
    0 讨论(0)
  • 2020-12-10 22:15

    Font properties are set on the Font object in the GUI object that you are using (JLabel, etc), not on the String itself.

    EDIT:

    If you want to add formatting to your console, you will have to embed the formatting within the String itself. In order to get my output to be bold, I needed to do the following:

     System.out.println((char)27 +"[1m testing bold");
    

    The (char) 27 is an escape sequence, the [ is followed by a set of ; separated values for different formatting types (see below), followed by an m. You will need to play around with this. On my Mac, the command prompt continued in bold as I didnt reset to normal ([0m) before I exited.

    This info, by the way, was lifted from here. some attributes:

    0 Normal (clear all)
    1 bold
    2 dim
    4 underline
    5 blink
    7 reverse
    8 blank
    9 overstrike
    22 normal intensity (cancel bold and blank)
    24 underline off
    25 blink off
    27 reverse off
    28 blank off
    29 overstrike off
    30 black
    31 red
    32 green
    33 yellow
    34 blue
    35 magenta
    36 cyan
    37 white
    40 black background
    41 red background
    42 green background
    
    0 讨论(0)
  • 2020-12-10 22:20

    I found another way to escape "\033" here

    Also, I wanted the official attributes list in regards to "akf's attribute chart which I found here - referenced in the "SGR (Select Graphic Rendition) parameters" table

    I would like to add that neither of these worked for me in eclipse console output. I take it that is impossible?

    0 讨论(0)
提交回复
热议问题