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

FontFamily - Combobox and TextBox (inputfield) #12

Closed
rufreakde opened this issue Jul 6, 2024 · 2 comments
Closed

FontFamily - Combobox and TextBox (inputfield) #12

rufreakde opened this issue Jul 6, 2024 · 2 comments

Comments

@rufreakde
Copy link

How is it possible to change the FontFamily of these controls? (and maybe some others) To achieve a consistent look I would like to modify their font but FontFamily is an unknown property.

Thanks in advance!

@Videogamers0
Copy link
Owner

Videogamers0 commented Jul 6, 2024

MGTextBox uses an MGTextBlock to render the text. You would want to do something like:

MGTextBox textBox = ...;
MGTextBlock textBlockComponent = textBox.TextBlockComponent.Element;
bool success = textBlockComponent.TrySetFont("familyname", fontSize);

Whichever family name you use must have already been registered to the FontManager via AddFontSet(...) instance method.

MGDesktop desktop = ...;
FontManager fm = desktop.FontManager;
fm.AddFontSet(new FontSet(...));

Note: If your MGTextBox is utilizing placeholder text (MGTextBox.PlaceholderText is not null), then you would probably also want to style the MGTextBox.PlaceholderTextComponent textblock.

For MGComboBox<T>, you would need to modify MGComboBox.DropdownItemTemplate and MGComboBox.SelectedItemTemplate. DropdownItemTemplate is invoked on each item in the combobox's ItemsSource to create the nested content. By default, it just creates an MGButton, and populates the button with a default MGTextBlock whose text is set to item.ToString(). You can override this behavior however you want. For example:

MGComboBox<double> comboBox = ...; // suppose we had a combobox whose ItemsSource is a collection of doubles
comboBox.DropdownItemTemplate = (item) =>
{
    string text = item.ToString("0.00"); // convert the decimal to a string with 2 decimal places
    MGButton button = comboBox.CreateDefaultDropdownButton(); // create the button that will host this combobox item

    //  Create the content of the button and customize its font family etc
    MGTextBlock textContent = new MGTextBlock(comboBox.ParentWindow, text);
    bool success = textContent.TrySetFont("fontFamily", 16);
    button.SetContent(textContent);

    return button;
};

There are also a couple settings named DefaultFontFamily that you could modify. One of them is an instance property in ThemeFontSettings:

MGDesktop desktop = ...;
MGTheme theme = desktop.Theme; //  Note: If the `MGWindow` specifies a theme, that theme would take priority. See also: MGElement.GetTheme()
theme.FontSettings.DefaultFontFamily = "fontFamilyName...";

The other one is an in instance property in FontManager.

MGDesktop desktop = ...;
FontManager fm = desktop.FontManager;
fm.DefaultFontFamily = "fontFamilyName...";

If a default is specified in both FontManager and MGTheme, the one in MGTheme takes precedence.

@rufreakde
Copy link
Author

Hi thanks,
for the detailed explanation! I changed the default font! Since I only need one font in my game!
image

Worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants