Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

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

Merged
merged 11 commits into from
Nov 6, 2018
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "Xamarin.Forms.Build"]
path = Xamarin.Forms.Build
url = ../../xamarin/Xamarin.Forms.Build
1 change: 0 additions & 1 deletion Xamarin.Forms.Build
Submodule Xamarin.Forms.Build deleted from 8f864c
Original file line number Diff line number Diff line change
@@ -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 = "This is center aligned
line 2.", HorizontalTextAlignment = TextAlignment.Center, LineHeight = 1.5 };
masonyc marked this conversation as resolved.
Show resolved Hide resolved

Content = new StackLayout()
{
Children = { label, label2 },
VerticalOptions = LayoutOptions.CenterAndExpand
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
32 changes: 25 additions & 7 deletions Xamarin.Forms.Platform.iOS/Renderers/FormattedStringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -53,8 +53,8 @@ public static NSAttributedString ToAttributed(this FormattedString formattedStri

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;
Expand All @@ -63,14 +63,30 @@ internal static NSAttributedString ToAttributed(this Span span, Element owner, C
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())
Expand Down Expand Up @@ -120,7 +136,7 @@ internal static NSAttributedString ToAttributed(this Span span, Element owner, C
}

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;
Expand All @@ -129,7 +145,9 @@ internal static NSAttributedString ToAttributed(this FormattedString formattedSt
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;

Expand Down
4 changes: 2 additions & 2 deletions Xamarin.Forms.Platform.iOS/Renderers/LabelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ void UpdateText()
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
}

Expand Down