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_gui2 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 133)
131fn main() {
132    nwg::init().expect("Failed to init Native Windows GUI");
133    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
134    let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI");
135    nwg::dispatch_thread_events();
136}
More examples
Hide additional examples
examples/basic_layout.rs (line 138)
136fn main() {
137    nwg::init().expect("Failed to init Native Windows GUI");
138    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
139    let _ui = BasicApp::build_ui(Default::default()).expect("Failed to build UI");
140    nwg::dispatch_thread_events();
141}
examples/calculator.rs (line 347)
345fn main() {
346    nwg::init().expect("Failed to init Native Windows GUI");
347    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
348
349    let _app = Calculator::build_ui(Default::default()).expect("Failed to build UI");
350    nwg::dispatch_thread_events();
351}
examples/flexbox.rs (line 169)
167fn main() {
168    nwg::init().expect("Failed to init Native Windows GUI");
169    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
170
171    let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI");
172
173    nwg::dispatch_thread_events();
174}
examples/flexbox_sub_layout.rs (line 159)
157fn main() {
158    nwg::init().expect("Failed to init Native Windows GUI");
159    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
160
161    let _ui = FlexBoxApp::build_ui(Default::default()).expect("Failed to build UI");
162
163    nwg::dispatch_thread_events();
164}
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}
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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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.