Enum embedded_text::style::HeightMode[][src]

pub enum HeightMode {
    Exact(VerticalOverdraw),
    FitToText,
    ShrinkToText(VerticalOverdraw),
}
Expand description

Specifies how the TextBox’s height should be adjusted.

Variants

Keep the original TextBox height.

Example:

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyleBuilder},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::Rectangle,
};
use embedded_text::{style::TextBoxStyleBuilder, TextBox};

let character_style = MonoTextStyleBuilder::new()
    .font(&FONT_6X9)
    .text_color(BinaryColor::On)
    .build();

// This TextBox contains two lines of text, but is 60px high
let text_box = TextBox::new(
    "Two lines\nof text",
    Rectangle::new(Point::zero(), Size::new(60, 60)),
    character_style,
);

// Exact does not change the size of the TextBox
let orig_size = text_box.bounding_box().size;
let size = text_box.bounding_box().size;
assert_eq!(size, orig_size);
FitToText

Sets the height of the TextBox to exactly fit the text.

Note: in this mode, vertical alignment is meaningless. Make sure to use [Top] alignment for efficiency.

Example: FitToText grows the TextBox.

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyleBuilder},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::Rectangle,
};
use embedded_text::{
    style::{HeightMode, TextBoxStyleBuilder},
    TextBox,
};

// Set style, use 6x9 MonoFont so the 2 lines are 18px high.
let character_style = MonoTextStyleBuilder::new()
    .font(&FONT_6X9)
    .text_color(BinaryColor::On)
    .build();

let style = TextBoxStyleBuilder::new().height_mode(HeightMode::FitToText).build();

// This TextBox contains two lines of text, but is 1px high
let text_box = TextBox::with_textbox_style(
    "Two lines\nof text",
    Rectangle::new(Point::zero(), Size::new(60, 0)),
    character_style,
    style,
);

// FitToText grows the TextBox to the height of the text
let size = text_box.bounding_box().size;
assert_eq!(size, Size::new(60, 18));

Example: FitToText also shrinks the TextBox.

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::Rectangle,
};
use embedded_text::{
    style::{HeightMode, TextBoxStyleBuilder},
    TextBox,
};

// Set style, use 6x9 MonoFont so the 2 lines are 18px high.
let character_style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On);

let style = TextBoxStyleBuilder::new().height_mode(HeightMode::FitToText).build();

// This TextBox contains two lines of text, but is 1px high
let text_box = TextBox::with_textbox_style(
    "Two lines\nof text",
    Rectangle::new(Point::zero(), Size::new(60, 60)),
    character_style,
    style,
);

// FitToText shrinks the TextBox to the height of the text
let size = text_box.bounding_box().size;
assert_eq!(size, Size::new(60, 18));
ShrinkToText(VerticalOverdraw)

If the text does not fill the bounding box, shrink the TextBox to be as tall as the text.

Example: ShrinkToText does not grow the TextBox.

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::Rectangle,
};
use embedded_text::{
    style::{HeightMode, VerticalOverdraw, TextBoxStyleBuilder},
    TextBox,
};

// Set style, use 6x9 MonoFont so the 2 lines are 18px high.
let character_style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On);

let style = TextBoxStyleBuilder::new()
    .height_mode(HeightMode::ShrinkToText(VerticalOverdraw::FullRowsOnly))
    .build();

// This TextBox contains two lines of text, but is 1px high
let text_box = TextBox::with_textbox_style(
    "Two lines\nof text",
    Rectangle::new(Point::zero(), Size::new(60, 0)),
    character_style,
    style,
);

let size = text_box.bounding_box().size;
assert_eq!(size, Size::new(60, 0));

Example: ShrinkToText shrinks the TextBox.

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::Rectangle,
};
use embedded_text::{
    style::{HeightMode, VerticalOverdraw, TextBoxStyleBuilder},
    TextBox,
};

// Set style, use 6x9 MonoFont so the 2 lines are 18px high.
let character_style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On);

let style = TextBoxStyleBuilder::new()
    .height_mode(HeightMode::ShrinkToText(VerticalOverdraw::FullRowsOnly))
    .build();

// This TextBox contains two lines of text, but is 60px high
let text_box = TextBox::with_textbox_style(
    "Two lines\nof text",
    Rectangle::new(Point::zero(), Size::new(60, 60)),
    character_style,
    style,
);

let size = text_box.bounding_box().size;
assert_eq!(size, Size::new(60, 18));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Casts the value.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Casts the value.

Performs the conversion.

Performs the conversion.

Casts the value.

Should always be Self

Casts the value.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Casts the value.

Casts the value.