Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for named fonts #113

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions examples/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cacao::view::{View, ViewDelegate};

struct BasicApp {
window: Window,
content_view: View<ContentView>
content_view: View<ContentView>,
}

impl AppDelegate for BasicApp {
Expand All @@ -29,36 +29,61 @@ impl AppDelegate for BasicApp {
#[derive(Default)]
struct ContentView {
content: View,
label: Label
label: Label,
label2: Label,
label3: Label,
}

impl ViewDelegate for ContentView {
const NAME: &'static str = "SafeAreaView";

fn did_load(&mut self, view: View) {
let color_black = cacao::color::Color::rgb(255, 255, 255);

let font = Font::system(30.);
self.label.set_font(&font);
self.label.set_text("Hello World");
self.label.set_text_color(cacao::color::Color::rgb(255, 255, 255));
self.label.set_text_color(&color_black);

let times_new_roman = Font::with_name("Times New Roman", 30.);
self.label2.set_font(&times_new_roman);
self.label2.set_text("Hello World (In 'Times New Roman')");
self.label2.set_text_color(&color_black);

let helvetica = Font::with_name("Helvetica", 30.);
self.label3.set_font(&helvetica);
self.label3.set_text("Hello World (In 'Helvetica')");
self.label3.set_text_color(&color_black);

self.content.add_subview(&self.label);
self.content.add_subview(&self.label2);
self.content.add_subview(&self.label3);
view.add_subview(&self.content);

// layouts for labels
cacao::layout::LayoutConstraint::activate(&[
self.label2.top.constraint_equal_to(&self.label.bottom).offset(2.),
self.label3.top.constraint_equal_to(&self.label2.bottom).offset(2.),
]);

// Add layout constraints to be 100% excluding the safe area
// Do last because it will crash because the view needs to be inside the hierarchy
cacao::layout::LayoutConstraint::activate(&[
self.content.top.constraint_equal_to(&view.safe_layout_guide.top),
self.content.leading.constraint_equal_to(&view.safe_layout_guide.leading),
self.content.trailing.constraint_equal_to(&view.safe_layout_guide.trailing),
self.content.bottom.constraint_equal_to(&view.safe_layout_guide.bottom)
self.content.bottom.constraint_equal_to(&view.safe_layout_guide.bottom),
])
}
}

fn main() {
App::new("com.test.window", BasicApp {
window: Window::default(),
content_view: View::with(ContentView::default())
})
App::new(
"com.test.window",
BasicApp {
window: Window::default(),
content_view: View::with(ContentView::default()),
},
)
.run();
}
7 changes: 7 additions & 0 deletions src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ impl Font {
Font(unsafe { msg_send_id![Self::class(), boldSystemFontOfSize: size] })
}

/// Creates and returns a font for the specified font name and size.
pub fn with_name(name: &str, size: f64) -> Self {
let font_name = NSString::new(name);
Font(unsafe { msg_send_id![Self::class(), fontWithName: &*font_name, size: size] })
}

/// Creates and returns a monospace system font at the specified size and weight
///
/// # Support
Expand Down Expand Up @@ -96,4 +102,5 @@ fn font_test() {
let default_font = Font::default();
let system_font = Font::system(100.0);
let bold_system_font = Font::bold_system(100.0);
let named_font = Font::with_name("Times New Roman", 100.0);
}