UXFont
UXFont is a font descriptor: a family, a point size, and the bold and
italic traits. A font chooser edits this value and text drawing carries it.
Glyph rasterisation and the list of available families belong to the backend. This type touches neither, so it works the same on GEM and macOS, and it can be compared, stored and passed around freely.
#use <UXKit> // or #import "UXFont.xc"Overview
Section titled “Overview”UXFont* base = UXFont.make((u8*)"Helvetica", 12);UXFont* head = base.bolded().withSize(18);Every derivation returns a new font. Nothing mutates:
base.bolded(); // Helvetica 12 Boldbase; // Helvetica 12 — unchangedThe type is built around this property. Whoever else holds a reference to a font handed to a label cannot change it underneath the label, so there is no defensive copying and no question about who owns a style.
Derivations chain, which is how a style menu composes:
base.bolded().italicized().withSize(14); // Helvetica 14 Bold ItalicTopics
Section titled “Topics”make · makeTraits · dup · withSize · withFamily · bolded · unbolded · italicized · togglingBold · togglingItalic · scaledBy · isBold · isItalic · isEqualTo · description
static UXFont* make(u8* family, i16 size)Family and point size, no traits. The default font is System 12.
makeTraits
Section titled “makeTraits”static UXFont* makeTraits(u8* family, i16 size, bool bold, bool italic)Sets all four at once. Use it to restore a saved font, where the traits are known rather than derived.
UXFont* dup(void)An independent copy. Every derivation below is dup plus one change, so
none of them can affect the receiver.
withSize
Section titled “withSize”UXFont* withSize(i16 s)withFamily
Section titled “withFamily”UXFont* withFamily(u8* fam)bolded / unbolded
Section titled “bolded / unbolded”UXFont* bolded(void)UXFont* unbolded(void)Set the trait, regardless of its previous value.
italicized
Section titled “italicized”UXFont* italicized(void)Sets italic on, regardless of its previous value.
togglingBold / togglingItalic
Section titled “togglingBold / togglingItalic”UXFont* togglingBold(void)UXFont* togglingItalic(void)Flip the trait. A Bold or Italic menu item uses this pair: the menu does not need to know the current state, and applying it twice returns the original font.
UXFont* b = base.togglingBold(); // Helvetica 12 Boldb.togglingBold(); // Helvetica 12scaledBy
Section titled “scaledBy”UXFont* scaledBy(i16 pct)A percentage of the current size, integer-rounded: scaledBy(150) on 12
gives 18. Use it to make text one step larger without knowing the base
size.
isBold / isItalic
Section titled “isBold / isItalic”bool isBold(void)bool isItalic(void)isEqualTo
Section titled “isEqualTo”bool isEqualTo(UXFont* o)Value equality: family, size and both traits. Two separately
constructed Helvetica 12s are equal:
UXFont* a = UXFont.make((u8*)"Helvetica", 12);UXFont* b = UXFont.make((u8*)"Helvetica", 12);a.isEqualTo(b) // truea.isEqualTo(a.bolded()) // falsedescription
Section titled “description”u8* description(void)A human-readable label such as "Helvetica 12 Bold Italic". Traits appear
only when set, so a plain font is "Helvetica 12". A chooser’s preview line
and a font menu item show this string.
Example
Section titled “Example”#import <Stdio.xc>#import "UXFont.xc"
void main(void) { UXFont* base = UXFont.make((u8*)"Helvetica", 12);
base.bolded(); // Helvetica 12 Bold base.italicized(); // Helvetica 12 Italic base.withSize(18); // Helvetica 18 base.scaledBy(150); // Helvetica 18 base; // Helvetica 12 — untouched
base.bolded().italicized().withSize(14); // Helvetica 14 Bold Italic
UXFont* b = base.togglingBold(); // Helvetica 12 Bold b.togglingBold(); // Helvetica 12
UXFont* other = UXFont.make((u8*)"Helvetica", 12); base.isEqualTo(other); // true base.isEqualTo(base.bolded()); // false}The full program is website/site/examples/uxkit/font.xc. The
doc-examples gate compiles it, and the comments are its real output.
Conforms to
Section titled “Conforms to”- A plain class (not an
Objectsubclass)
See also
Section titled “See also”UXTextLayout: measuring and wrapping text in a fontUXAttributedString: runs of text that each carry their own fontUXGraphics: where a font becomes glyphs