[iOS] Label HorizontalTextAlignment="Center" not working in conjunction with LineHeight on iOS (#4275)

* fix issue#4262
Label HorizontalTextAlignment="Center" not working in conjunction with LineHeight on iOS

* Add missing parameter for macOS due to the previous method param change

* merge

* Added using UITextAlignment = AppKit.NSTextAlignment; as maintainability requested

* Revert "merge"

This reverts commit 939b6e1bc8ebd538af0db91dd8656e7eb2d31203.

* Revert "merge"

This reverts commit 939b6e1bc8ebd538af0db91dd8656e7eb2d31203.

* make test case more clear
This commit is contained in:
masonyc 2018-11-07 11:34:28 +13:00 коммит произвёл E.Z. Hart
Родитель 9c1c7aa542
Коммит af0bc40eec
5 изменённых файлов: 51 добавлений и 10 удалений

@ -1 +1 @@
Subproject commit dcf2726eac6b47c64555ddb19966aa40ef31d1fa
Subproject commit 8f864cf6b0c9e7b1973bee04e5fd81933067163e

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

@ -0,0 +1,22 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 4262, "Label HorizontalTextAlignment=\"Center\" not working in conjunction with LineHeight on iOS", PlatformAffected.iOS)]
public class Issue4262 : ContentPage
{
public Issue4262()
{
var label = new Label() { Text = "This is center aligned
line 2.", HorizontalTextAlignment = TextAlignment.Center };
var label2 = new Label() { Text = "If this is not center aligned, this test has failed.", HorizontalTextAlignment = TextAlignment.Center, LineHeight = 1.5 };
Content = new StackLayout()
{
Children = { label, label2 },
VerticalOptions = LayoutOptions.CenterAndExpand
};
}
}
}

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

@ -406,6 +406,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue4136.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue4262.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LegacyComponents\NonAppCompatSwitch.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MapsModalCrash.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModalActivityIndicatorTest.cs" />

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

@ -8,7 +8,7 @@ namespace Xamarin.Forms.Platform.iOS
#else
using AppKit;
using UIColor = AppKit.NSColor;
using UITextAlignment = AppKit.NSTextAlignment;
namespace Xamarin.Forms.Platform.MacOS
#endif
{
@ -53,8 +53,8 @@ namespace Xamarin.Forms.Platform.MacOS
return attributed;
}
internal static NSAttributedString ToAttributed(this Span span, Element owner, Color defaultForegroundColor, double lineHeight = -1.0)
internal static NSAttributedString ToAttributed(this Span span, Element owner, Color defaultForegroundColor, TextAlignment textAlignment, double lineHeight = -1.0)
{
if (span == null)
return null;
@ -63,14 +63,30 @@ namespace Xamarin.Forms.Platform.MacOS
if (text == null)
return null;
NSMutableParagraphStyle style = null;
NSMutableParagraphStyle style = new NSMutableParagraphStyle();
lineHeight = span.LineHeight >= 0 ? span.LineHeight : lineHeight;
if (lineHeight >= 0)
{
style = new NSMutableParagraphStyle();
style.LineHeightMultiple = new nfloat(lineHeight);
}
switch (textAlignment)
{
case TextAlignment.Start:
style.Alignment = UITextAlignment.Left;
break;
case TextAlignment.Center:
style.Alignment = UITextAlignment.Center;
break;
case TextAlignment.End:
style.Alignment = UITextAlignment.Right;
break;
default:
style.Alignment = UITextAlignment.Left;
break;
}
#if __MOBILE__
UIFont targetFont;
if (span.IsDefault())
@ -120,7 +136,7 @@ namespace Xamarin.Forms.Platform.MacOS
}
internal static NSAttributedString ToAttributed(this FormattedString formattedString, Element owner,
Color defaultForegroundColor, double lineHeight = -1.0)
Color defaultForegroundColor, TextAlignment textAlignment = TextAlignment.Start, double lineHeight = -1.0)
{
if (formattedString == null)
return null;
@ -129,7 +145,9 @@ namespace Xamarin.Forms.Platform.MacOS
for (int i = 0; i < formattedString.Spans.Count; i++)
{
Span span = formattedString.Spans[i];
var attributedString = span.ToAttributed(owner, defaultForegroundColor, lineHeight);
var attributedString = span.ToAttributed(owner, defaultForegroundColor, textAlignment, lineHeight);
if (attributedString == null)
continue;

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

@ -328,9 +328,9 @@ namespace Xamarin.Forms.Platform.MacOS
void UpdateFormattedText()
{
#if __MOBILE__
Control.AttributedText = _formatted.ToAttributed(Element, Element.TextColor, Element.LineHeight);
Control.AttributedText = _formatted.ToAttributed(Element, Element.TextColor, Element.HorizontalTextAlignment, Element.LineHeight);
#else
Control.AttributedStringValue = _formatted.ToAttributed(Element, Element.TextColor, Element.LineHeight);
Control.AttributedStringValue = _formatted.ToAttributed(Element, Element.TextColor, Element.HorizontalTextAlignment, Element.LineHeight);
#endif
}