From 90a9b6b57ed9fa73d8bbcf421b92fd1f41ce587d Mon Sep 17 00:00:00 2001 From: YJK Date: Wed, 3 Apr 2019 10:14:13 +0800 Subject: [PATCH] =?UTF-8?q?YTextBlock=E4=BF=AE=E5=A4=8DBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YUI.Test/MainWindow.xaml | 2 +- YUI/Properties/AssemblyInfo.cs | 4 +- YUI/YControls/YTextBlock.cs | 89 +++++++++++++--------------------- YUI/YUI.WPF.nuspec | 2 +- 4 files changed, 37 insertions(+), 60 deletions(-) diff --git a/YUI.Test/MainWindow.xaml b/YUI.Test/MainWindow.xaml index 84939ba..3aecf19 100644 --- a/YUI.Test/MainWindow.xaml +++ b/YUI.Test/MainWindow.xaml @@ -14,7 +14,7 @@ - + SetValue(TextProperty, value); } - private FormattedText FormattedText { get; set; } - private List FormattedTexts { get; set; } /// @@ -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(); - FormattedTexts = new List(); + 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); } /// @@ -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)); } } } diff --git a/YUI/YUI.WPF.nuspec b/YUI/YUI.WPF.nuspec index 40f00e1..98ca92d 100644 --- a/YUI/YUI.WPF.nuspec +++ b/YUI/YUI.WPF.nuspec @@ -11,7 +11,7 @@ https://icon false $description$ - YTextBlock添加TextWrapping + YTextBlock修复BUG Copyright 2019 WPF Tool