Skip to main content

Font

Struct Font 

Source
pub struct Font {
    pub handle: HFONT,
}
Expand description

Represent a system font.

Can be used with any controls that draws text. Due to the very limited way win32 can draw text, only family, size and weight can be configured.

Example:

use native_windows_gui as nwg;

fn build_font() -> nwg::Font {
    let mut font = nwg::Font::default();

    nwg::Font::builder()
        .size(16)
        .family("Arial")
        .weight(1000)
        .build(&mut font);

    font
}

Fields§

§handle: HFONT

Implementations§

Source§

impl Font

Source

pub fn builder<'a>() -> FontBuilder<'a>

Source

pub fn set_global_default(font: Option<Font>) -> Option<Font>

Set the default (application global!) font that will be used when creating controls and return the old one

Source

pub fn set_global_family(family: &str) -> Result<Option<Font>, NwgError>

Set the default (application global!) font that will be used when creating controls This is a shortcut over Font::set_global_default

Examples found in repository?
examples/basic.rs (line 123)
121fn main() {
122    nwg::init().expect("Failed to init Native Windows GUI");
123    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
124    let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI");
125    nwg::dispatch_thread_events();
126}
More examples
Hide additional examples
examples/basic_layout.rs (line 128)
126fn main() {
127    nwg::init().expect("Failed to init Native Windows GUI");
128    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
129    let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI");
130    nwg::dispatch_thread_events();
131}
examples/calculator.rs (line 283)
281fn main() {
282    nwg::init().expect("Failed to init Native Windows GUI");
283    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
284
285    let _app = Calculator::build_ui(Default::default()).expect("Failed to build UI");
286    nwg::dispatch_thread_events();
287}
examples/message_bank.rs (line 182)
180fn main() {
181    nwg::init().expect("Failed to init Native Windows GUI");
182    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
183
184    let _ui = MessageBank::build_ui(Default::default()).expect("Failed to build UI");
185    nwg::dispatch_thread_events();
186}
examples/flexbox.rs (line 144)
142fn main() {
143    nwg::init().expect("Failed to init Native Windows GUI");
144    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
145
146    let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI");
147    
148    nwg::dispatch_thread_events();
149}
examples/flexbox_sub_layout.rs (line 145)
143fn main() {
144    nwg::init().expect("Failed to init Native Windows GUI");
145    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
146
147    let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI");
148    
149    nwg::dispatch_thread_events();
150}
Source

pub fn global_default() -> Option<Font>

Return the default font that was previously set using Font::set_default

Source

pub fn add_font(path: &str) -> bool

Add a font to the system font table. Don’t forget to call Font::remove_font(path) once you’re done. Returns false if the font could not be added. Windows won’t tell you why though.

Other info:

  • The value of path can be a ttf or a otf font.
  • Adding the same font multiple time increase the internal refcount
  • Use Font::families() to return the available system font families
Source

pub fn remove_font(path: &str)

Remove a font that was previously added by Font::add_font

Source

pub fn add_memory_font(bin: &mut [u8]) -> Result<MemFont, ()>

Add a font resource from a binary source. Returns a memory font handle if the font was loaded succesfully. Send the handle to remove_memory_font at the end of your program to free the font from memory.

Source

pub fn remove_memory_font(font: MemFont)

Remove a font that was previously added by Font::add_memory_font

Source

pub fn families() -> Vec<String>

Returns all the font families loaded on the OS. Probably pretty slow, so cache the value if possible

Trait Implementations§

Source§

impl Debug for Font

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Font

Source§

fn default() -> Font

Returns the “default value” for a type. Read more
Source§

impl Eq for Font

Source§

impl PartialEq for Font

Source§

fn eq(&self, other: &Font) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Send for Font

Source§

impl StructuralPartialEq for Font

Source§

impl Sync for Font

Auto Trait Implementations§

§

impl Freeze for Font

§

impl RefUnwindSafe for Font

§

impl Unpin for Font

§

impl UnsafeUnpin for Font

§

impl UnwindSafe for Font

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.