// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using Perspex.Media; using Perspex.Platform; using Splat; public class TextBlock : Control { public static readonly PerspexProperty TextProperty = PerspexProperty.Register("Text"); public TextBlock() { this.GetObservable(TextProperty).Subscribe(_ => this.InvalidateVisual()); } public string Text { get { return this.GetValue(TextProperty); } set { this.SetValue(TextProperty, value); } } private FormattedText FormattedText { get { return new FormattedText { FontFamilyName = "Segoe UI", FontSize = this.FontSize, Text = this.Text, }; } } public override void Render(IDrawingContext context) { if (this.Visibility == Visibility.Visible) { Brush background = this.Background; if (background != null) { context.FillRectange(background, new Rect(this.ActualSize)); } context.DrawText(this.Foreground, new Rect(this.ActualSize), this.FormattedText); } } protected override Size MeasureOverride(Size availableSize) { if (this.Visibility != Visibility.Collapsed) { if (!string.IsNullOrEmpty(this.Text)) { return this.FormattedText.Size; } } return new Size(); } } }