Skip to content

Commit 91f3fd6

Browse files
committed
[iOS] HTML Label not applying Bold or Italics (#20372)
1 parent 2046c5e commit 91f3fd6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Core/src/Handlers/Label/LabelHandler.iOS.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public static void MapFont(ILabelHandler handler, ILabel label)
6363
var fontManager = handler.GetRequiredService<IFontManager>();
6464

6565
handler.PlatformView?.UpdateFont(label, fontManager);
66+
67+
if (handler.PlatformView?.AttributedText != null)
68+
handler.PlatformView?.UpdateTextHtml(label);
6669
}
6770

6871
public static void MapLineHeight(ILabelHandler handler, ILabel label)

src/Core/src/Platform/iOS/LabelExtensions.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,31 @@ internal static void UpdateTextHtml(this UILabel platformLabel, ILabel label)
8383
StringEncoding = NSStringEncoding.UTF8
8484
};
8585

86+
var uiFontAttribute = label?.Handler?.GetRequiredService<IFontManager>()?.GetFont(label.Font, UIFont.LabelFontSize);
87+
8688
NSError nsError = new();
87-
#pragma warning disable CS8601
88-
platformLabel.AttributedText = new NSAttributedString(text, attr, ref nsError);
89-
#pragma warning restore CS8601
89+
var attributedString = new NSMutableAttributedString(new NSAttributedString(text, attr, ref nsError));
90+
91+
// Enumerate through the attributes in the string and update font size
92+
attributedString.EnumerateAttributes(new NSRange(0, attributedString.Length), NSAttributedStringEnumeration.None,
93+
(NSDictionary attrs, NSRange range, ref bool stop) =>
94+
{
95+
if (label!.Font.Family == null)
96+
{
97+
var font = attrs[UIStringAttributeKey.Font] as UIFont;
98+
if (font != null)
99+
{
100+
font = font.WithSize((nfloat)(label?.Font.Size ?? UIFont.LabelFontSize));
101+
attributedString.AddAttribute(UIStringAttributeKey.Font, font, range);
102+
}
103+
}
104+
else if (uiFontAttribute != null)
105+
{
106+
attributedString.AddAttribute(UIStringAttributeKey.Font, uiFontAttribute, range);
107+
}
108+
});
109+
110+
platformLabel.AttributedText = attributedString;
90111
}
91112

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

0 commit comments

Comments
 (0)