-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS] Html label not applying bold or italics #20488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kubaflo
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
kubaflo:html-label-not-applying-bold-or-italics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+37.2 KB
...d.Tests/snapshots/android/BothHtmlLabelsShouldApplyBoldAndItalicaProperties.png
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
46
src/Controls/tests/TestCases.HostApp/Issues/Issue20372.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
15
src/Controls/tests/TestCases.HostApp/Issues/Issue20372.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20372.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } | ||
Binary file added
BIN
+5.54 KB
...I.Tests/snapshots/windows/BothHtmlLabelsShouldApplyBoldAndItalicaProperties.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+43 KB
...s.iOS.Tests/snapshots/ios/BothHtmlLabelsShouldApplyBoldAndItalicaProperties.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.