Fix Label TextDecorations issue on iOS (#10066) fixes #8272

This commit is contained in:
Javier Suárez 2020-03-26 12:06:38 +01:00 коммит произвёл GitHub
Родитель e70bc55df7
Коммит fbae244b35
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 98 добавлений и 4 удалений

Просмотреть файл

@ -0,0 +1,94 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 8272, "[Bug] TextDecorations don't work", PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.Label)]
#endif
public class Issue8272 : TestContentPage
{
public Issue8272()
{
#if APP
Title = "Issue 8272";
#endif
}
protected override void Init()
{
var layout = new StackLayout
{
Padding = new Thickness(12)
};
var instructions = new Label
{
BackgroundColor = Color.Black,
TextColor = Color.White,
Text = "Toggle the Switch. If the text below is underline, the test has passed."
};
Grid grid = new Grid { Margin = new Thickness(20) };
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
ScrollView scrollView = new ScrollView();
Label label = new Label
{
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
CharacterSpacing = 2,
Text = "The Switch should toggle underline on this Label."
};
scrollView.Content = label;
Label underlineLabel = new Label { Text = "Underline:", VerticalOptions = LayoutOptions.Center };
Switch underlineSwitch = new Switch { VerticalOptions = LayoutOptions.Center };
Label characterSpacingLabel = new Label { Text = "CharacterSpacing:", VerticalOptions = LayoutOptions.Center };
Slider characterSpacingSlider = new Slider
{
Maximum = 24,
Minimum = 0,
Value = 2,
VerticalOptions = LayoutOptions.Center
};
grid.Children.Add(underlineLabel, 0, 0);
grid.Children.Add(underlineSwitch, 1, 0);
grid.Children.Add(characterSpacingLabel, 0, 1);
grid.Children.Add(characterSpacingSlider, 1, 1);
grid.Children.Add(scrollView, 0, 2);
Grid.SetColumnSpan(scrollView, 4);
layout.Children.Add(instructions);
layout.Children.Add(grid);
underlineSwitch.Toggled += (sender, args) =>
{
if (underlineSwitch.IsToggled)
label.TextDecorations = TextDecorations.Underline;
else
label.TextDecorations = TextDecorations.None;
};
characterSpacingSlider.ValueChanged += (sender, args) =>
{
label.CharacterSpacing = characterSpacingSlider.Value;
};
Content = layout;
}
}
}

Просмотреть файл

@ -1292,6 +1292,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue9054.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue9306.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue9417.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8272.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Bugzilla22229.xaml">

Просмотреть файл

@ -250,9 +250,6 @@ namespace Xamarin.Forms.Platform.MacOS
if (Element?.TextType != TextType.Text)
return;
if (!Element.IsSet(Label.TextDecorationsProperty))
return;
#if __MOBILE__
if (!(Control.AttributedText?.Length > 0))
return;
@ -284,7 +281,9 @@ namespace Xamarin.Forms.Platform.MacOS
else
newAttributedText.AddAttribute(underlineStyleKey, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);
#if !__MOBILE__
#if __MOBILE__
Control.AttributedText = newAttributedText;
#else
Control.AttributedStringValue = newAttributedText;
#endif
UpdateCharacterSpacing();