1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00
MLEM/MLEM/Extensions/SpriteFontExtensions.cs

25 lines
929 B
C#
Raw Normal View History

2019-08-06 14:20:11 +02:00
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
namespace MLEM.Extensions {
public static class SpriteFontExtensions {
public static IEnumerable<string> SplitString(this SpriteFont font, string text, float width, float scale) {
var builder = new StringBuilder();
2019-08-24 00:07:54 +02:00
foreach (var line in text.Split('\n')) {
foreach (var word in line.Split(' ')) {
builder.Append(word).Append(' ');
if (font.MeasureString(builder).X * scale >= width) {
var len = builder.Length - word.Length - 1;
yield return builder.ToString(0, len - 1);
builder.Remove(0, len);
}
2019-08-06 14:20:11 +02:00
}
2019-08-24 00:07:54 +02:00
yield return builder.ToString(0, builder.Length - 1);
builder.Clear();
2019-08-06 14:20:11 +02:00
}
}
}
}