Skip to content
12 changes: 12 additions & 0 deletions src/Controls/src/Core/Hosting/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ internal static IMauiHandlersCollection AddControlsHandlers(this IMauiHandlersCo
handlersCollection.AddHandler<CollectionView, CollectionViewHandler>();
handlersCollection.AddHandler<CarouselView, CarouselViewHandler>();
#endif
#if ANDROID
if (RuntimeFeature.IsMaterial3Enabled)
{
handlersCollection.AddHandler<Label, LabelHandler2>();
}
else
{
handlersCollection.AddHandler<Label, LabelHandler>();
}
#else
handlersCollection.AddHandler<Label, LabelHandler>();
#endif
#if ANDROID
if (RuntimeFeature.IsMaterial3Enabled)
{
Expand Down
47 changes: 36 additions & 11 deletions src/Controls/src/Core/Label/Label.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,45 @@ namespace Microsoft.Maui.Controls
public partial class Label
{
MauiTextView _mauiTextView;
MauiMaterialTextView _mauiMaterialTextView;

private protected override void OnHandlerChangedCore()
{
base.OnHandlerChangedCore();

if (Handler != null)
// Detach from previous views
DetachLayoutChanged();

if (Handler is not ILabelHandler { PlatformView: var platformView })
{
return;
}

switch (platformView)
{
if (Handler is LabelHandler labelHandler && labelHandler.PlatformView is MauiTextView mauiTextView)
{
_mauiTextView = mauiTextView;
case MauiTextView textView:
_mauiTextView = textView;
_mauiTextView.LayoutChanged += OnLayoutChanged;
}
break;
case MauiMaterialTextView materialTextView:
_mauiMaterialTextView = materialTextView;
_mauiMaterialTextView.LayoutChanged += OnLayoutChanged;
break;
}
else
}

void DetachLayoutChanged()
{
if (_mauiTextView is not null)
{
if (_mauiTextView != null)
{
_mauiTextView.LayoutChanged -= OnLayoutChanged;
_mauiTextView = null;
}
_mauiTextView.LayoutChanged -= OnLayoutChanged;
_mauiTextView = null;
}

if (_mauiMaterialTextView is not null)
{
_mauiMaterialTextView.LayoutChanged -= OnLayoutChanged;
_mauiMaterialTextView = null;
}
}

Expand All @@ -54,19 +73,25 @@ public static void MapMaxLines(ILabelHandler handler, Label label)
void OnLayoutChanged(object sender, LayoutChangedEventArgs args)
{
if (Handler is not ILabelHandler labelHandler)
{
return;
}

var platformView = labelHandler.PlatformView;
var virtualView = labelHandler.VirtualView as Label;

if (platformView == null || virtualView == null)
{
return;
}

var text = virtualView.FormattedText;

// don't attempt to layout if this is not a formatted text WITH some text
if (virtualView.TextType != TextType.Text || text?.Spans == null || text.Spans.Count == 0)
{
return;
}

var spannableString = virtualView.ToSpannableString();

Expand Down
1 change: 0 additions & 1 deletion src/Controls/src/Core/Label/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.Maui.Controls
/// <summary>A <see cref="Microsoft.Maui.Controls.View"/> that displays text.</summary>
[ContentProperty(nameof(Text))]
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
[ElementHandler<LabelHandler>]
public partial class Label : View, IFontElement, ITextElement, ITextAlignmentElement, ILineHeightElement, IElementConfiguration<Label>, IDecorableTextElement, IPaddingElement, ILabel
{
/// <summary>Bindable property for <see cref="HorizontalTextAlignment"/>.</summary>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading