csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.0 KiB
66 lines
2.0 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="TextBlock.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex.Controls
|
|
{
|
|
using Perspex.Media;
|
|
using Splat;
|
|
|
|
public class TextBlock : Control
|
|
{
|
|
public static readonly PerspexProperty<double> FontSizeProperty =
|
|
PerspexProperty.Register<TextBlock, double>(
|
|
"FontSize",
|
|
defaultValue: 12.0,
|
|
inherits: true);
|
|
|
|
public static readonly PerspexProperty<string> TextProperty =
|
|
PerspexProperty.Register<Border, string>("Text");
|
|
|
|
public double FontSize
|
|
{
|
|
get { return this.GetValue(FontSizeProperty); }
|
|
set { this.SetValue(FontSizeProperty, value); }
|
|
}
|
|
|
|
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)
|
|
{
|
|
Brush background = this.Background;
|
|
|
|
if (background != null)
|
|
{
|
|
context.FillRectange(background, this.Bounds);
|
|
}
|
|
|
|
context.DrawText(this.Foreground, new Rect(this.Bounds.Size), this.FormattedText);
|
|
}
|
|
|
|
protected override Size MeasureContent(Size availableSize)
|
|
{
|
|
ITextService service = Locator.Current.GetService<ITextService>();
|
|
return service.Measure(this.FormattedText);
|
|
}
|
|
}
|
|
}
|
|
|