Skip to content

Commit 74c0ed0

Browse files
authored
Correctly implement font style cascading in nested text (microsoft#1800)
* Correctly implement font style cascading in nested text * Applied same logic to the properties applied to the Span directly (in the associated view manager)
1 parent 74301e5 commit 74c0ed0

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

ReactWindows/ReactNative.Shared/Views/Text/ReactSpanShadowNode.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using ReactNative.Reflection;
@@ -23,7 +23,7 @@ namespace ReactNative.Views.Text
2323
public class ReactSpanShadowNode : ReactInlineShadowNode
2424
{
2525
private double? _fontSize;
26-
private int _letterSpacing;
26+
private int? _letterSpacing;
2727

2828
private FontStyle? _fontStyle;
2929
private FontWeight? _fontWeight;
@@ -99,13 +99,11 @@ public void SetFontStyle(string fontStyleValue)
9999
/// </summary>
100100
/// <param name="letterSpacing">The letter spacing.</param>
101101
[ReactProp(ViewProps.LetterSpacing)]
102-
public void SetLetterSpacing(int letterSpacing)
102+
public void SetLetterSpacing(int? letterSpacing)
103103
{
104-
var spacing = 50*letterSpacing; // TODO: Find exact multiplier (50) to match iOS
105-
106-
if (_letterSpacing != spacing)
104+
if (_letterSpacing != letterSpacing)
107105
{
108-
_letterSpacing = spacing;
106+
_letterSpacing = letterSpacing;
109107
MarkUpdated();
110108
}
111109
}
@@ -135,15 +133,51 @@ public override Inline MakeInline(IList<Inline> children)
135133
public override void UpdateInline(Inline inline)
136134
{
137135
#if WINDOWS_UWP
138-
inline.CharacterSpacing = _letterSpacing;
139-
inline.FontStyle = _fontStyle ?? FontStyle.Normal;
140-
inline.FontFamily = _fontFamily != null ? new FontFamily(_fontFamily) : FontFamily.XamlAutoFontFamily;
141-
#else
142-
inline.FontStyle = _fontStyle ?? new FontStyle();
143-
inline.FontFamily = _fontFamily != null ? new FontFamily(_fontFamily) : new FontFamily();
136+
if (_letterSpacing.HasValue)
137+
{
138+
var spacing = 50 * _letterSpacing.Value; // TODO: Find exact multiplier (50) to match iOS
139+
inline.CharacterSpacing = spacing;
140+
}
141+
else
142+
{
143+
inline.ClearValue(Inline.CharacterSpacingProperty);
144+
}
144145
#endif
145-
inline.FontSize = _fontSize ?? 15;
146-
inline.FontWeight = _fontWeight ?? FontWeights.Normal;
146+
if (_fontStyle.HasValue)
147+
{
148+
inline.FontStyle = _fontStyle.Value;
149+
}
150+
else
151+
{
152+
inline.ClearValue(Inline.FontStyleProperty);
153+
}
154+
155+
if (!string.IsNullOrEmpty(_fontFamily))
156+
{
157+
inline.FontFamily = new FontFamily(_fontFamily);
158+
}
159+
else
160+
{
161+
inline.ClearValue(Inline.FontFamilyProperty);
162+
}
163+
164+
if (_fontSize.HasValue)
165+
{
166+
inline.FontSize = _fontSize.Value;
167+
}
168+
else
169+
{
170+
inline.ClearValue(Inline.FontSizeProperty);
171+
}
172+
173+
if (_fontWeight.HasValue)
174+
{
175+
inline.FontWeight = _fontWeight.Value;
176+
}
177+
else
178+
{
179+
inline.ClearValue(Inline.FontWeightProperty);
180+
}
147181
}
148182

149183
/// <summary>

ReactWindows/ReactNative.Shared/Views/Text/ReactSpanViewManager.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ public bool NeedsCustomLayoutForChildren
5656
[ReactProp(ViewProps.Color, CustomType = "Color")]
5757
public void SetColor(Span view, uint? color)
5858
{
59-
view.Foreground = color.HasValue
60-
? new SolidColorBrush(ColorHelpers.Parse(color.Value))
61-
: null;
59+
if (color.HasValue)
60+
{
61+
view.Foreground = new SolidColorBrush(ColorHelpers.Parse(color.Value));
62+
}
63+
else
64+
{
65+
view.ClearValue(Span.ForegroundProperty);
66+
}
6267
}
6368

6469
#if !WINDOWS_UWP
@@ -70,23 +75,30 @@ public void SetColor(Span view, uint? color)
7075
[ReactProp(ViewProps.TextDecorationLine)]
7176
public void SetTextDecorationLine(Span view, string textDecorationLineValue)
7277
{
73-
var textDecorationLine = EnumHelpers.ParseNullable<TextDecorationLine>(textDecorationLineValue) ?? TextDecorationLine.None;
78+
var textDecorationLine = EnumHelpers.ParseNullable<TextDecorationLine>(textDecorationLineValue);
7479

75-
switch (textDecorationLine)
80+
if (textDecorationLine.HasValue)
81+
{
82+
switch (textDecorationLine.Value)
83+
{
84+
case TextDecorationLine.Underline:
85+
view.TextDecorations = TextDecorations.Underline;
86+
break;
87+
case TextDecorationLine.LineThrough:
88+
view.TextDecorations = TextDecorations.Strikethrough;
89+
break;
90+
case TextDecorationLine.UnderlineLineThrough:
91+
view.TextDecorations = new TextDecorationCollection(TextDecorations.Underline.Concat(TextDecorations.Strikethrough));
92+
break;
93+
case TextDecorationLine.None:
94+
default:
95+
view.TextDecorations = null;
96+
break;
97+
}
98+
}
99+
else
76100
{
77-
case TextDecorationLine.Underline:
78-
view.TextDecorations = TextDecorations.Underline;
79-
break;
80-
case TextDecorationLine.LineThrough:
81-
view.TextDecorations = TextDecorations.Strikethrough;
82-
break;
83-
case TextDecorationLine.UnderlineLineThrough:
84-
view.TextDecorations = new TextDecorationCollection(TextDecorations.Underline.Concat(TextDecorations.Strikethrough));
85-
break;
86-
case TextDecorationLine.None:
87-
default:
88-
view.TextDecorations = null;
89-
break;
101+
view.ClearValue(Span.TextDecorationsProperty);
90102
}
91103
}
92104
#endif

0 commit comments

Comments
 (0)