Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Controls/src/Core/Label/Label.Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public partial class Label
LabelHandler.Mapper.ModifyMapping<Label, ILabelHandler>(nameof(LineHeight), MapLineHeight);
#endif

#if IOS
LabelHandler.Mapper.AppendToMapping<Label, ILabelHandler>(nameof(ILabel.Font), MapHtmlText);
#endif

// platform-specifics
#if WINDOWS
LabelHandler.Mapper.ReplaceMapping<Label, ILabelHandler>(PlatformConfiguration.WindowsSpecific.InputView.DetectReadingOrderFromContentProperty.PropertyName, MapDetectReadingOrderFromContent);
Expand Down
8 changes: 8 additions & 0 deletions src/Controls/src/Core/Label/Label.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public static void MapText(ILabelHandler handler, Label label)
MapFormatting(handler, label);
}

internal static void MapHtmlText(ILabelHandler handler, Label label)
{
if (handler.PlatformView?.AttributedText != null)
{
handler.PlatformView?.UpdateTextHtml(label);
}
}

public static void MapLineBreakMode(ILabelHandler handler, Label label)
{
handler.PlatformView?.UpdateLineBreakMode(label);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20372.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue20372"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<ContentPage.Resources>
<Style x:Key="PrimaryStyle"
TargetType="Label">
<Setter Property="FontSize" Value="22" />
<Setter Property="TextColor" Value="MediumPurple" />
</Style>
</ContentPage.Resources>

<VerticalStackLayout Margin="20">

<Label TextType="Html"
AutomationId="label1"
x:Name="label1"
Style="{StaticResource PrimaryStyle}">
<Label.Text>
<![CDATA[
<strong>Bold Text</strong><br>
<em>italics</em><br>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
]]>
</Label.Text>
</Label>

<Label TextType="Html">
<Label.Text>
<![CDATA[
<strong>Bold Text</strong><br>
<em>italics</em><br>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
]]>
</Label.Text>
</Label>

</VerticalStackLayout>
</ContentPage>
15 changes: 15 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue20372.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 20372, "[iOS] HTML Label not applying Bold or Italics on iOS", PlatformAffected.iOS)]
public partial class Issue20372 : ContentPage
{
public Issue20372()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue20372 : _IssuesUITest
{
public override string Issue => "[iOS] HTML Label not applying Bold or Italics on iOS";

public Issue20372(TestDevice device) : base(device)
{
}

[Test]
[Category(UITestCategories.WebView)]
public void BothHtmlLabelsShouldApplyBoldAndItalicaProperties()
{
_ = App.WaitForElement("label1");

// Both labels should have proper text attributes
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 30 additions & 3 deletions src/Core/src/Platform/iOS/LabelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,37 @@ internal static void UpdateTextHtml(this UILabel platformLabel, ILabel label)
#endif
};

var uiFontAttribute = label?.Handler?.GetRequiredService<IFontManager>()?.GetFont(label.Font, UIFont.LabelFontSize);

NSError nsError = new();
#pragma warning disable CS8601
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);
#pragma warning restore CS8601
var attributedString = new NSMutableAttributedString(new NSAttributedString(text, attr, ref nsError));

// Enumerate through the attributes in the string and update font size
attributedString.EnumerateAttributes(new NSRange(0, attributedString.Length), NSAttributedStringEnumeration.None,
(NSDictionary attrs, NSRange range, ref bool stop) =>
{
if (label!.Font.Family == null)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is only applied on default font family? What if users using custom font?

{
var font = attrs[UIStringAttributeKey.Font] as UIFont;
if (font != null)
{
font = font.WithSize((nfloat)(label?.Font.Size ?? UIFont.LabelFontSize));
attributedString.AddAttribute(UIStringAttributeKey.Font, font, range);
}
}
else if (uiFontAttribute != null)
{
attributedString.AddAttribute(UIStringAttributeKey.Font, uiFontAttribute, range);
}

if(label?.TextColor != null)
{
var color = label.TextColor.ToPlatform();
attributedString.AddAttribute(UIStringAttributeKey.ForegroundColor, color, range);
}
});

platformLabel.AttributedText = attributedString;
}

internal static void UpdateTextPlainText(this UILabel platformLabel, IText label)
Expand Down