Skip to main content

text_and_image/
text_and_image.rs

1#[path = "support/mod.rs"]
2mod support;
3
4use embedded_graphics::{
5    image::{ImageRaw, ImageRawBE},
6    pixelcolor::Rgb565,
7    prelude::{Point, RgbColor, Size},
8    primitives::Rectangle,
9};
10use faststep::{
11    EdgeInsets, ImageAlignment, ImageView, ImageViewStyle, RichTextView, TextAlignment,
12    TextRunStyle, TextSpan, TextVerticalAlignment, TextView, TextViewStyle, TextWrap,
13};
14
15fn main() {
16    let mut canvas = support::NullCanvas::new(Size::new(320, 240));
17
18    let image_data = [
19        0xF8, 0x00, 0x07, 0xE0, //
20        0x07, 0xE0, 0xF8, 0x00, //
21    ];
22    let image: ImageRawBE<Rgb565> = ImageRaw::new(&image_data, 2);
23
24    ImageView::new(
25        Rectangle::new(Point::new(16, 16), Size::new(72, 72)),
26        &image,
27    )
28    .with_style(
29        ImageViewStyle::new()
30            .with_alignment(ImageAlignment::Center)
31            .with_insets(EdgeInsets::all(8))
32            .with_background(Rgb565::new(28, 58, 28))
33            .with_border(Rgb565::new(20, 44, 20), 2)
34            .with_corner_radius(12),
35    )
36    .draw(&mut canvas);
37
38    TextView::new(
39        Rectangle::new(Point::new(104, 18), Size::new(192, 28)),
40        "Faststep",
41        TextRunStyle::title(Rgb565::BLACK),
42    )
43    .with_view_style(
44        TextViewStyle::new()
45            .with_alignment(TextAlignment::Leading)
46            .with_vertical_alignment(TextVerticalAlignment::Center),
47    )
48    .draw(&mut canvas);
49
50    let spans = [
51        TextSpan::new("UIKit-like ", TextRunStyle::body(Rgb565::BLACK)),
52        TextSpan::new("embedded UI", TextRunStyle::body_strong(Rgb565::BLUE)),
53        TextSpan::new(
54            "\nfor devices and simulators",
55            TextRunStyle::caption(Rgb565::GREEN),
56        ),
57    ];
58    RichTextView::new(
59        Rectangle::new(Point::new(104, 58), Size::new(192, 72)),
60        &spans,
61    )
62    .with_view_style(
63        TextViewStyle::new()
64            .with_wrap(TextWrap::Multiline)
65            .with_insets(EdgeInsets::all(4)),
66    )
67    .draw(&mut canvas);
68}