Package swingtree
Class UI.Font
java.lang.Object
swingtree.UI.Font
- Enclosing class:
UI
An immutable and value based font configuration object, which is used to configure component fonts
in a UI declaration. SwingTree can dynamically generate
This is a wrapper around a
We recommend using this class instead of classical AWT Font instances in your code, as it allows SwingTree to scale your UI more reliably.
Fonts for
components using the toAwtFont() method. This may be done reactively, like for
example when the look and feel changes its UI scale and SwingTree scales and re-installs your fonts.This is a wrapper around a
FontConf object, which is also used in the UIForAnySwing.withStyle(Styler)
API, and it holds all font properties needed to create Font instances for Swing components.
The appearance of a font is primarily based on the font family name which is used to find a font on the system,
but there is a variety of other properties which you can configure using with(Configurator).We recommend using this class instead of classical AWT Font instances in your code, as it allows SwingTree to scale your UI more reliably.
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionconf()Returns the configuration of this font which encapsulates all properties of the font and can be used to convert to aFontobject using theFontConf.toAwtFont()method.booleaninthashCode()static UI.Fontstatic UI.FontA factory method that creates a newFontobject with the specified font name and size where theUI.FontStyledefaults toPLAIN.static UI.Fontof(String name, UI.FontStyle style, int size) A factory method that creates a newFontobject with the specified font nameUI.FontStyleand size.static UI.Fontof(Map<? extends AttributedCharacterIterator.Attribute, ?> attributes) Creates a newFontobject from a map of attributes where the key is an attribute and the value is the value of the attribute.Converts this font to aFontobject that can be used in Swing components.toString()with(Configurator<FontConf> configurator) Allows configuring this font using a configurator function to a newFontobject with the desired changes.Returns an updated version of this font with the font (family) name changed to the specified value.withSize(int size) Returns an updated version of this font with the font size changed to the specified value.withStyle(UI.FontStyle style) Returns an updated version of this font with the font style changed to the specified value.
-
Field Details
-
UNDEFINED
This constant is aFontobject with a font name of "" (empty string), a font style of -1 (undefined) and a font size of 0. Its identity is used to represent the absence of a font being specified, and it is used as a safe replacement for null, meaning that when the style engine of a component encounters it, it will pass it onto theComponent.setFont(java.awt.Font)method as null. Passing null to this method means that the look and feel determines the font.
-
-
Method Details
-
of
A factory method that creates a newFontobject with the specified font nameUI.FontStyleand size. This maps directly to the constructor ofFont(String, int, int).- Parameters:
name- The font name, which may be anything depending on what fonts are loaded on the system.style- The style of the font, which is one of the constantsUI.FontStyle.PLAIN,size- The point size of the font.- Returns:
- A new
Fontobject with the specified font name, style and size.
-
of
A factory method that creates a newFontobject with the specified font name and size where theUI.FontStyledefaults toPLAIN. This maps directly to the constructor ofFont(String, int, int).- Parameters:
name- The font name, which may be anything depending on what fonts are loaded on the system.size- The point size of the font.- Returns:
- A new
Fontobject with the specified font name, style and size.
-
of
Creates a newFontobject from a map of attributes where the key is an attribute and the value is the value of the attribute. SeeTextAttributefor a list of common attributes. These attributes define the style of the font.- Parameters:
attributes- A map of attributes that define the style of the font.- Returns:
- A new
Fontobject with the specified attributes.
-
of
- Parameters:
font- The font to convert to a font.- Returns:
- The SwingTree native font object.
-
toAwtFont
Converts this font to aFontobject that can be used in Swing components. Note that this method creates a newFontobject which will always be scaled according to the current DPI settings of SwingTree, which you can access usingUI.scale().- Returns:
- The
Fontobject representing this font.
-
conf
Returns the configuration of this font which encapsulates all properties of the font and can be used to convert to aFontobject using theFontConf.toAwtFont()method.- Returns:
- The configuration of this font.
-
with
Allows configuring this font using a configurator function to a newFontobject with the desired changes.- Parameters:
configurator- The configurator function that applies changes to the font configuration. It receives aFontConfobject representing the current configuration of the font and should return the modifiedFontConfobject.- Returns:
- A new
Fontobject with the applied changes. - Throws:
NullPointerException- If the configurator is null.
-
withName
Returns an updated version of this font with the font (family) name changed to the specified value.- Parameters:
name- The font name, which may be anything depending on what fonts are loaded on the system.- Returns:
- A new
Fontobject with the font name changed. - Throws:
NullPointerException- If the font name is null.
-
withStyle
Returns an updated version of this font with the font style changed to the specified value.- Parameters:
style- The style of the font, which is one of the constantsUI.FontStyle.PLAIN,UI.FontStyle.BOLDorUI.FontStyle.ITALIC.- Returns:
- A new
Fontobject with the font style changed.
-
withSize
Returns an updated version of this font with the font size changed to the specified value.- Parameters:
size- The point size of the font.- Returns:
- A new
Fontobject with the font size changed.
-
hashCode
public int hashCode() -
equals
-
toString
-