Skip to content

Commit

Permalink
YTextBlock修复BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
YJammak committed Apr 3, 2019
1 parent d25ed22 commit 90a9b6b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
2 changes: 1 addition & 1 deletion YUI.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<StackPanel DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
Margin="10">
<Grid Height="400" Width="200" Background="Red">
<yctr:YTextBlock TextWrapping="True" Padding="10" Text="保持车距&#13;谨慎驾驶&#13;注意安全" LineSpacing="10" CharacterSpacing="10" Foreground="BlueViolet" Stroke="Aqua" StrokeThickness="2" FontSize="50" FontWeight="Bold"/>
<yctr:YTextBlock CharacterSpacing="10" TextWrapping="True" Padding="10" Text="保持车距&#13;谨慎驾驶&#13;注意安全" Foreground="BlueViolet" Stroke="Aqua" StrokeThickness="2" FontSize="50" FontWeight="Bold"/>
</Grid>
<yctr:YTailBorder Background="#2A60B0" Placement="LeftCenter"
HorizontalAlignment="Center" CornerRadius="3"
Expand Down
4 changes: 2 additions & 2 deletions YUI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.4")]
[assembly: AssemblyFileVersion("1.3.4")]
[assembly: AssemblyVersion("1.3.5")]
[assembly: AssemblyFileVersion("1.3.5")]
89 changes: 33 additions & 56 deletions YUI/YControls/YTextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ public string Text
set => SetValue(TextProperty, value);
}

private FormattedText FormattedText { get; set; }

private List<YFormattedTextInfo> FormattedTexts { get; set; }

/// <inheritdoc />
Expand Down Expand Up @@ -399,60 +397,49 @@ private Size GetVerticalFormattedText(string str)

private Size GetHorizontalFormattedText(string str)
{
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (CharacterSpacing == 0.0 || str == "")
{
FormattedText = new FormattedText(str, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black);
var lines = str.Split(new[] {"\n", "\r", "\r\n"}, StringSplitOptions.None);

return new Size(FormattedText.Width + Padding.Left + Padding.Right, FormattedText.Height + Padding.Top + Padding.Bottom);
}
else
{
var lines = str.Split(new[] {"\n", "\r", "\r\n"}, StringSplitOptions.None);
var width = 0.0;
var height = 0.0;
var y = 0.0;

var width = 0.0;
var height = 0.0;
var y = 0.0;
FormattedTexts = new List<YFormattedTextInfo>();

FormattedTexts = new List<YFormattedTextInfo>();
for (var i = 0; i < lines.Length; i++)
{
var lineWidth = 0.0;
var currentLine = lines[i];

for (var i = 0; i < lines.Length; i++)
for (var j = 0; j < currentLine.Length; j++)
{
var lineWidth = 0.0;
var currentLine = lines[i];

for (var j = 0; j < currentLine.Length; j++)
{
var character = currentLine[j].ToString();
var character = currentLine[j].ToString();

var formattedText = new FormattedText(character, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black);
var formattedText = new FormattedText(character, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black);

if (j == 0 && i != 0)
y = height + LineSpacing;
if (j == 0 && i != 0)
y = height + LineSpacing;

var x = j == 0 ? 0.0 : lineWidth + CharacterSpacing;
var x = j == 0 ? 0.0 : lineWidth + CharacterSpacing;

lineWidth = x + formattedText.Width;
width = Math.Max(width, lineWidth);
height = Math.Max(height, formattedText.Height + y);
lineWidth = x + formattedText.Width;
width = Math.Max(width, lineWidth);
height = Math.Max(height, formattedText.Height + y);

var point = new Point(x, y);
var point = new Point(x, y);

var formattedInfo = new YFormattedTextInfo
{
FormattedText = formattedText,
Point = point,
Text = character,
};
var formattedInfo = new YFormattedTextInfo
{
FormattedText = formattedText,
Point = point,
Text = character,
};

FormattedTexts.Add(formattedInfo);
}
FormattedTexts.Add(formattedInfo);
}

return new Size(width + Padding.Left + Padding.Right, height + Padding.Top + Padding.Bottom);
}

return new Size(width + Padding.Left + Padding.Right, height + Padding.Top + Padding.Bottom);
}

/// <inheritdoc />
Expand All @@ -464,21 +451,11 @@ protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (CharacterSpacing == 0.0 && Orientation == Orientation.Horizontal)
foreach (var info in FormattedTexts)
{
var point = new Point(Padding.Left, Padding.Top);
drawingContext.DrawText(FormattedText, point);
drawingContext.DrawGeometry(Foreground, new Pen(Stroke, StrokeThickness), FormattedText.BuildGeometry(point));
}
else
{
foreach (var info in FormattedTexts)
{
var point = new Point(Padding.Left + info.Point.X, Padding.Top + info.Point.Y);
drawingContext.DrawText(info.FormattedText, point);
drawingContext.DrawGeometry(Foreground, new Pen(Stroke, StrokeThickness), info.FormattedText.BuildGeometry(point));
}
var point = new Point(Padding.Left + info.Point.X, Padding.Top + info.Point.Y);
drawingContext.DrawText(info.FormattedText, point);
drawingContext.DrawGeometry(Foreground, new Pen(Stroke, StrokeThickness), info.FormattedText.BuildGeometry(point));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion YUI/YUI.WPF.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<iconUrl>https://icon</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>YTextBlock添加TextWrapping</releaseNotes>
<releaseNotes>YTextBlock修复BUG</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>WPF Tool</tags>
</metadata>
Expand Down

0 comments on commit 90a9b6b

Please sign in to comment.