Font 不是节点,因为它仅仅描述了文本的样式。Font 不能显示在屏幕上,也不能进行旋转之类的操作。
1 2 3 4 5 6 7 8 9 10 11 12 13
auto text = gcnew Text(L"Hello Easy2D!");
// 创建一个字体,宋体、字号40、粗体、斜体 Font font = Font(); font.family = L"宋体"; font.size = 40; font.weight = Font::Weight::Bold; font.italic = true; // 也可以用下面的代码创建字体,效果和上面一样 Font font = Font(L"宋体", 40, Font::Weight::Bold, true);
// 设置字体 text->setFont(font);
分步骤进行上面的操作虽然简洁明了,但是熟悉了相关操作后,上面的代码难免显得有些啰嗦。你可以直接在 Text 的构造函数中传入更多的参数来指定它的字体样式:
1 2
// 在创建文本的同时指定它的字体样式 auto text = gcnew Text(L"Hello Easy2D!", Font(L"宋体", 40, Font::Weight::Bold));
Tips
没有设置 Text 的字体时,它会自动创建一个默认字体并使用。
多个 Text 可以使用一个 Font,例如下面的代码中 text1 和 text2 使用了同一个 Font 对象。
text1 和 text2 在内部会各自拷贝一份 Font,所以创建文字后再修改 font 不会影响 text1 和 text2。
1 2 3
Font font = Font(L"", 40); // 系统默认字体、字号40 auto text1 = gcnew Text(L"Hello", font); // text1 使用 font auto text2 = gcnew Text(L"Easy2D", font); // text2 也使用 font