Example #1
0
        public void DrawText(string text, Rectangle destinationRectangle, TextStyle style)
        {
            if (string.IsNullOrEmpty(text))
                return;

            var textPosition = AlignText(text, destinationRectangle, style);

            _fontRenderer.DrawText(_spriteBatch, textPosition.X, textPosition.Y, text, style.Colour);
        }
Example #2
0
        private Point AlignText(string text, Rectangle destinationRectangle, TextStyle style)
        {
            int x = 0, y = 0;
            var size = _fontRenderer.MeasureText(text);
            var centre = destinationRectangle.Center;
            var lineHeight = _fontRenderer.FontFile.Common.LineHeight;

            switch (style.HorizontalAlignment)
            {
                case HorizontalAlignment.Centre:
                    x = centre.X - size.Width / 2;
                    break;
                case HorizontalAlignment.Stretch:
                case HorizontalAlignment.Left:
                    x = destinationRectangle.X;
                    break;
                case HorizontalAlignment.Right:
                    x = destinationRectangle.Right - size.Width;
                    break;
            }

            switch (style.VerticalAlignment)
            {
                case VerticalAlignment.Centre:
                    y = centre.Y - lineHeight / 2;
                    break;
                case VerticalAlignment.Stretch:
                case VerticalAlignment.Top:
                    y = destinationRectangle.Y;
                    break;
                case VerticalAlignment.Bottom:
                    y = destinationRectangle.Bottom - size.Height;
                    break;
            }

            return new Point(x, y);
        }
Example #3
0
 public TextControl(VisualStyle defaultStyle)
     : base(defaultStyle)
 {
     TextStyle = new TextStyle();
 }