1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-16 02:53:11 +01:00
MLEM/Docs/articles/font_extensions.md

33 lines
1.9 KiB
Markdown
Raw Normal View History

# Font Extensions
2020-05-21 01:08:36 +02:00
The **MLEM** base package features several additional font manipulation methods, including line splitting and text truncating. These abilities can be accessed through generic fonts.
## Generic fonts
2021-11-08 02:50:53 +01:00
MLEM features the `GenericFont` class along with a `GenericSpriteFont` implementation. This is used by the **MLEM.Ui** package, but it can also be used separately to have more control over font rendering.
2020-05-21 01:08:36 +02:00
2021-11-08 02:50:53 +01:00
Additionally, the **MLEM.Extended** package provides the following generic fonts:
- `GenericBitmapFont`, which uses [MonoGame.Extended](http://www.monogameextended.net/)'s `SpriteFont`
- `GenericStashFont`, which uses [FontStashSharp](https://github.com/rds1983/FontStashSharp)'s fonts
2020-05-21 01:08:36 +02:00
### Creating a generic font
2021-11-08 02:50:53 +01:00
To create a generic font, simply create an instance of the desired generic font class when loading your game:
2020-05-21 01:08:36 +02:00
```cs
// Using MonoGame SpriteFonts
var spriteFont = new GenericSpriteFont(this.Content.Load<SpriteFont>("Fonts/ExampleFont"));
```
## Line splitting
2020-05-21 01:08:36 +02:00
Using generic fonts, a long line of text can be split into multiple lines based on a maximum width in pixels. The split text that is returned is delimited by `\n` (newline characters).
```cs
var split = spriteFont.SplitString("This is a really long line of text [...]", width: 100, scale: 1);
spriteFont.DrawString(this.SpriteBatch, split, new Vector2(10, 10), Color.White);
```
## Truncating
2020-05-21 01:08:36 +02:00
Using generic fonts, a long line of text can also be truncated to fit a certain width in pixels. The remaining text that doesn't fit will simply be chopped off of the end (or start) of the string.
```cs
// Truncate from the front
var truncFront = spriteFont.TruncateString("This is a really long line of text [...]", width: 100, fromBack: false, scale: 1);
// Truncate from the back
var truncBack = spriteFont.TruncateString("This is a really long line of text [...]", width: 100, fromBack: true, scale: 1);
```