Skip to content

Commit 8e8eea4

Browse files
authored
Implement MaxLines property in LabelHandlers (#457)
* Updated LabelStub * Fix build error * Fix merge issue * Added PortHandler attributes
1 parent b4e3c63 commit 8e8eea4

File tree

14 files changed

+59
-2
lines changed

14 files changed

+59
-2
lines changed

Diff for: src/Compatibility/Core/src/Android/Extensions/TextViewExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
88
{
99
internal static class TextViewExtensions
1010
{
11+
[PortHandler("Partially ported")]
1112
public static void SetMaxLines(this TextView textView, Label label)
1213
{
1314
var maxLines = label.MaxLines;

Diff for: src/Compatibility/Core/src/Android/FastRenderers/LabelRenderer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ void UpdateLineBreakMode()
371371
_lastSizeRequest = null;
372372
}
373373

374+
[PortHandler]
374375
void UpdateMaxLines()
375376
{
376377
this.SetMaxLines(Element);

Diff for: src/Compatibility/Core/src/iOS/Renderers/LabelRenderer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ void UpdateLayout()
603603
#endif
604604
}
605605

606+
[PortHandler("Partially ported")]
606607
void UpdateMaxLines()
607608
{
608609
if (Element.MaxLines >= 0)

Diff for: src/Core/src/Core/ILabel.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace Microsoft.Maui
55
/// </summary>
66
public interface ILabel : IView, IText
77
{
8+
/// <summary>
9+
/// Gets the maximum number of lines allowed in the Label.
10+
/// </summary>
11+
int MaxLines { get; }
12+
813
/// <summary>
914
/// Gets the space between the text of the Label and it's border.
1015
/// </summary>

Diff for: src/Core/src/Handlers/Label/LabelHandler.Android.cs

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static void MapCharacterSpacing(LabelHandler handler, ILabel label)
3737
handler.TypedNativeView?.UpdateCharacterSpacing(label);
3838
}
3939

40+
public static void MapMaxLines(LabelHandler handler, ILabel label)
41+
{
42+
handler.TypedNativeView?.UpdateMaxLines(label);
43+
}
44+
4045
public static void MapPadding(LabelHandler handler, ILabel label)
4146
{
4247
handler.TypedNativeView?.UpdatePadding(label);

Diff for: src/Core/src/Handlers/Label/LabelHandler.Standard.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public static void MapText(IViewHandler handler, ILabel label) { }
1010
public static void MapTextColor(IViewHandler handler, ILabel label) { }
1111
public static void MapCharacterSpacing(IViewHandler handler, ILabel label) { }
1212
public static void MapFont(LabelHandler handler, ILabel label) { }
13-
public static void MapPadding(LabelHandler handler, ILabel label) { }
1413
public static void MapTextDecorations(LabelHandler handler, ILabel label) { }
14+
public static void MapMaxLines(IViewHandler handler, ILabel label) { }
15+
public static void MapPadding(LabelHandler handler, ILabel label) { }
1516
}
1617
}

Diff for: src/Core/src/Handlers/Label/LabelHandler.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public partial class LabelHandler
77
[nameof(ILabel.TextColor)] = MapTextColor,
88
[nameof(ILabel.Text)] = MapText,
99
[nameof(ILabel.CharacterSpacing)] = MapCharacterSpacing,
10+
[nameof(ILabel.MaxLines)] = MapMaxLines,
1011
[nameof(ILabel.Font)] = MapFont,
1112
[nameof(ILabel.Padding)] = MapPadding,
1213
[nameof(ILabel.TextDecorations)] = MapTextDecorations

Diff for: src/Core/src/Handlers/Label/LabelHandler.iOS.cs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static void MapCharacterSpacing(LabelHandler handler, ILabel label)
2323
handler.TypedNativeView?.UpdateCharacterSpacing(label);
2424
}
2525

26+
public static void MapMaxLines(LabelHandler handler, ILabel label)
27+
{
28+
handler.TypedNativeView?.UpdateMaxLines(label);
29+
}
30+
2631
public static void MapPadding(LabelHandler handler, ILabel label)
2732
{
2833
handler.TypedNativeView?.UpdatePadding(label);

Diff for: src/Core/src/Platform/Android/LabelExtensions.cs

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public static void UpdateFont(this TextView textView, ILabel label, IFontManager
3939
textView.SetTextSize(ComplexUnitType.Sp, sp);
4040
}
4141

42+
public static void UpdateMaxLines(this TextView textView, ILabel label)
43+
{
44+
int maxLinex = label.MaxLines;
45+
46+
textView.SetMaxLines(maxLinex);
47+
}
48+
4249
public static void UpdatePadding(this TextView textView, ILabel label)
4350
{
4451
var context = textView.Context;

Diff for: src/Core/src/Platform/iOS/LabelExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public static void UpdateFont(this UILabel nativeLabel, ILabel label, IFontManag
4747
nativeLabel.UpdateCharacterSpacing(label);
4848
}
4949

50+
public static void UpdateMaxLines(this UILabel nativeLabel, ILabel label)
51+
{
52+
int maxLines = label.MaxLines;
53+
54+
if (maxLines >= 0)
55+
{
56+
nativeLabel.Lines = maxLines;
57+
}
58+
}
59+
5060
public static void UpdatePadding(this MauiLabel nativeLabel, ILabel label)
5161
{
5262
nativeLabel.TextInsets = new UIEdgeInsets(

Diff for: src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.Android.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task FontFamilyInitializesCorrectly(string family)
3838
Assert.NotEqual(fontManager.DefaultTypeface, nativeLabel.Typeface);
3939
}
4040

41-
[Fact]
41+
[Fact(DisplayName = "Padding Initializes Correctly")]
4242
public async Task PaddingInitializesCorrectly()
4343
{
4444
var label = new LabelStub()
@@ -109,6 +109,9 @@ bool GetNativeIsBold(LabelHandler labelHandler) =>
109109
bool GetNativeIsItalic(LabelHandler labelHandler) =>
110110
GetNativeLabel(labelHandler).Typeface.IsItalic;
111111

112+
int GetNativeMaxLines(LabelHandler labelHandler) =>
113+
GetNativeLabel(labelHandler).MaxLines;
114+
112115
Task ValidateNativeBackgroundColor(ILabel label, Color color)
113116
{
114117
return InvokeOnMainThreadAsync(() =>

Diff for: src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,17 @@ await ValidateUnrelatedPropertyUnaffected(
148148
nameof(ILabel.Text),
149149
() => label.Text = newText);
150150
}
151+
152+
[Fact(DisplayName = "MaxLines Initializes Correctly")]
153+
public async Task MaxLinesInitializesCorrectly()
154+
{
155+
var label = new LabelStub()
156+
{
157+
Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
158+
MaxLines = 2
159+
};
160+
161+
await ValidatePropertyInitValue(label, () => label.MaxLines, GetNativeMaxLines, label.MaxLines);
162+
}
151163
}
152164
}

Diff for: src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.iOS.cs

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ bool GetNativeIsBold(LabelHandler labelHandler) =>
9595
bool GetNativeIsItalic(LabelHandler labelHandler) =>
9696
GetNativeLabel(labelHandler).Font.FontDescriptor.SymbolicTraits.HasFlag(UIFontDescriptorSymbolicTraits.Italic);
9797

98+
int GetNativeMaxLines(LabelHandler labelHandler) =>
99+
(int)GetNativeLabel(labelHandler).Lines;
100+
98101
double GetNativeCharacterSpacing(LabelHandler labelHandler)
99102
{
100103
var nativeLabel = GetNativeLabel(labelHandler);

Diff for: src/Core/tests/DeviceTests/Stubs/LabelStub.cs

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public partial class LabelStub : StubBase, ILabel
1313
public Font Font { get; set; }
1414

1515
public TextDecorations TextDecorations { get; set; }
16+
17+
public int MaxLines { get; set; } = -1;
1618
}
1719
}

0 commit comments

Comments
 (0)