mirror of
https://github.com/ckaczor/ChrisKaczor.Wpf.Controls.HtmlTextBlock.git
synced 2026-01-27 09:35:41 -05:00
Initial commit
This commit is contained in:
64
TextFragment.cs
Normal file
64
TextFragment.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace ChrisKaczor.Wpf.Controls
|
||||
{
|
||||
public class TextFragment
|
||||
{
|
||||
private readonly HtmlTextBlock _parent;
|
||||
|
||||
public Brush Color { get; set; }
|
||||
public FontStyle Style { get; set; }
|
||||
public FontWeight Weight { get; set; }
|
||||
public double Size { get; set; }
|
||||
public string Text { get; set; }
|
||||
public bool Underline { get; set; }
|
||||
|
||||
private Typeface? _typeface;
|
||||
|
||||
private Typeface Typeface
|
||||
{
|
||||
get { return _typeface ??= new Typeface(_parent.FontFamily, Style, Weight, _parent.FontStretch); }
|
||||
}
|
||||
|
||||
private FormattedText? _formattedText;
|
||||
public FormattedText FormattedText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_formattedText != null)
|
||||
return _formattedText;
|
||||
|
||||
var measureText = string.IsNullOrEmpty(Text) ? " " : Text;
|
||||
|
||||
_formattedText = new FormattedText(measureText, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, Typeface, Size, Color, null, TextFormattingMode.Display, 1);
|
||||
|
||||
var textDecorationCollection = new TextDecorationCollection();
|
||||
|
||||
if (Underline)
|
||||
{
|
||||
var underlineDecoration = new TextDecoration { PenThicknessUnit = TextDecorationUnit.FontRecommended };
|
||||
|
||||
textDecorationCollection.Add(underlineDecoration);
|
||||
}
|
||||
|
||||
_formattedText.SetTextDecorations(textDecorationCollection);
|
||||
|
||||
return _formattedText;
|
||||
}
|
||||
}
|
||||
|
||||
public TextFragment(HtmlTextBlock parent)
|
||||
{
|
||||
_parent = parent;
|
||||
|
||||
Color = _parent.Foreground;
|
||||
Style = _parent.FontStyle;
|
||||
Weight = _parent.FontWeight;
|
||||
Size = _parent.FontSize;
|
||||
Text = string.Empty;
|
||||
Underline = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user