-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Correctly re-apply font formatting to HTML text #14701
Changes from all commits
dfd12f7
22b11a0
956d0dc
f4ca7f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,12 @@ namespace Microsoft.Maui.Controls | |
{ | ||
public partial class Label | ||
{ | ||
public static void MapTextType(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); | ||
public static void MapTextType(LabelHandler handler, Label label) => MapTextType((ILabelHandler)handler, label); | ||
public static void MapText(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); | ||
|
||
public static void MapTextType(ILabelHandler handler, Label label) | ||
{ | ||
Platform.TextExtensions.UpdateText(handler.PlatformView, label); | ||
handler.UpdateValue(nameof(ILabel.Text)); | ||
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. Instead of calling the same method, we should be triggering the Text property so people can properly intercept it. |
||
} | ||
|
||
public static void MapText(ILabelHandler handler, Label label) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
#nullable disable | ||
using System; | ||
using Microsoft.Maui.Controls.Platform; | ||
using Microsoft.Maui.Handlers; | ||
|
||
namespace Microsoft.Maui.Controls | ||
{ | ||
public partial class Label | ||
{ | ||
public static void MapTextType(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); | ||
public static void MapTextType(LabelHandler handler, Label label) => MapTextType((ILabelHandler)handler, label); | ||
public static void MapText(LabelHandler handler, Label label) => MapText((ILabelHandler)handler, label); | ||
public static void MapCharacterSpacing(LabelHandler handler, Label label) => MapCharacterSpacing((ILabelHandler)handler, label); | ||
public static void MapTextDecorations(LabelHandler handler, Label label) => MapTextDecorations((ILabelHandler)handler, label); | ||
|
@@ -17,61 +17,48 @@ public partial class Label | |
|
||
public static void MapTextType(ILabelHandler handler, Label label) | ||
{ | ||
Platform.LabelExtensions.UpdateText(handler.PlatformView, label); | ||
handler.UpdateValue(nameof(ILabel.Text)); | ||
} | ||
|
||
public static void MapText(ILabelHandler handler, Label label) | ||
{ | ||
Platform.LabelExtensions.UpdateText(handler.PlatformView, label); | ||
|
||
MapFormatting(handler, label); | ||
Comment on lines
+26
to
+27
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. This is the real iOS fix that re-applied formatting. |
||
} | ||
|
||
public static void MapTextDecorations(ILabelHandler handler, Label label) | ||
{ | ||
if (label?.HasFormattedTextSpans ?? false) | ||
return; | ||
|
||
if (label?.TextType == TextType.Html) | ||
{ | ||
if (!IsPlainText(label)) | ||
return; | ||
} | ||
|
||
LabelHandler.MapTextDecorations(handler, label); | ||
} | ||
|
||
public static void MapCharacterSpacing(ILabelHandler handler, Label label) | ||
{ | ||
if (label?.HasFormattedTextSpans ?? false) | ||
return; | ||
|
||
if (label?.TextType == TextType.Html) | ||
{ | ||
if (!IsPlainText(label)) | ||
return; | ||
} | ||
|
||
LabelHandler.MapCharacterSpacing(handler, label); | ||
} | ||
|
||
public static void MapLineHeight(ILabelHandler handler, Label label) | ||
{ | ||
if (label?.HasFormattedTextSpans ?? false) | ||
return; | ||
|
||
if (label?.TextType == TextType.Html) | ||
{ | ||
if (!IsPlainText(label)) | ||
return; | ||
} | ||
|
||
LabelHandler.MapLineHeight(handler, label); | ||
} | ||
|
||
public static void MapFont(ILabelHandler handler, Label label) | ||
{ | ||
if (label?.HasFormattedTextSpans ?? false) | ||
if (label.HasFormattedTextSpans) | ||
return; | ||
|
||
if (label?.TextType == TextType.Html && FontIsDefault(label)) | ||
if (label.TextType == TextType.Html && IsDefaultFont(label)) | ||
{ | ||
// If no explicit font has been specified and we're displaying HTML, | ||
// If no explicit font has been specified and we're displaying HTML, | ||
// let the HTML determine the font | ||
return; | ||
} | ||
|
@@ -81,12 +68,12 @@ public static void MapFont(ILabelHandler handler, Label label) | |
|
||
public static void MapTextColor(ILabelHandler handler, Label label) | ||
{ | ||
if (label?.HasFormattedTextSpans ?? false) | ||
if (label.HasFormattedTextSpans) | ||
return; | ||
|
||
if (label?.TextType == TextType.Html && label.GetValue(TextColorProperty) == null) | ||
if (label.TextType == TextType.Html && label.TextColor.IsDefault()) | ||
{ | ||
// If no explicit text color has been specified and we're displaying HTML, | ||
// If no explicit text color has been specified and we're displaying HTML, | ||
// let the HTML determine the colors | ||
return; | ||
} | ||
|
@@ -104,22 +91,33 @@ public static void MapMaxLines(ILabelHandler handler, Label label) | |
handler.PlatformView?.UpdateMaxLines(label); | ||
} | ||
|
||
static bool FontIsDefault(Label label) | ||
static void MapFormatting(ILabelHandler handler, Label label) | ||
{ | ||
handler.UpdateValue(nameof(ILabel.TextColor)); | ||
handler.UpdateValue(nameof(ILabel.Font)); | ||
} | ||
Comment on lines
+94
to
+98
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. Call the properties that need to be re-applied. |
||
|
||
static bool IsDefaultFont(Label label) | ||
{ | ||
if (label.IsSet(Label.FontAttributesProperty)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (label.IsSet(Label.FontFamilyProperty)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (label.IsSet(Label.FontSizeProperty)) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static bool IsPlainText(Label label) | ||
{ | ||
if (label.HasFormattedTextSpans) | ||
return false; | ||
|
||
if (label.TextType != TextType.Text) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,13 +73,13 @@ public static void MapLineHeight(ILabelHandler handler, ILabel label) | |
public static void MapFormatting(ILabelHandler handler, ILabel label) | ||
{ | ||
// Update all of the attributed text formatting properties | ||
handler.PlatformView?.UpdateLineHeight(label); | ||
handler.PlatformView?.UpdateTextDecorations(label); | ||
handler.PlatformView?.UpdateCharacterSpacing(label); | ||
handler.UpdateValue(nameof(ILabel.LineHeight)); | ||
handler.UpdateValue(nameof(ILabel.TextDecorations)); | ||
handler.UpdateValue(nameof(ILabel.CharacterSpacing)); | ||
|
||
// Setting any of those may have removed text alignment settings, | ||
// so we need to make sure those are applied, too | ||
handler.PlatformView?.UpdateHorizontalTextAlignment(label); | ||
handler.UpdateValue(nameof(ILabel.HorizontalTextAlignment)); | ||
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. Don't just call the method, trigger the property so that the mappers can re-run. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed as the new way of
Platform.TextViewExtensions.UpdateText()
correctly handles span colors. There is no difference with either HTML of formatted text.