Skip to content
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

Allow text content for Label #455

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions samples/ControlGallery/Views/Controls/Labels.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@page "/controls/labels"

<ContentPage>
<ScrollView>
<VerticalStackLayout Padding="10" Spacing="20">

<Label Text="Static label with Text property" />

<Label>Static label with child content</Label>

<Label Text=@($"Dynamic - {RandomText} - label with Text property") />

<Label>Dynamic - @RandomText - label with child content</Label>

<Label>
<Span Text="Label with spans " />
<Span TextColor="Colors.Red">with color, </Span>
<Span TextDecorations="TextDecorations.Strikethrough" Text="text decorations" />
<Span FontAttributes="FontAttributes.Bold" Text=" and font attributes." />
</Label>

<Label Text="Label with bigger font size" FontSize="24" />

</VerticalStackLayout>
</ScrollView>
</ContentPage>

@code {
Random _random = new();

string RandomText => _random.Next().ToString();
}
1 change: 1 addition & 0 deletions samples/ControlGallery/Views/PlaygroundList.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Label Text="Select a playground..." FontSize="30" FontAttributes="FontAttributes.Bold" />
<BoxView Margin="10" HeightRequest="2" Color="Colors.DarkGray" HorizontalOptions="LayoutOptions.FillAndExpand" />
<Button Text="Common controls" OnClick="@(async () => await NavigationManager.NavigateToAsync("/commoncontrols"))"></Button>
<Button Text="Labels" OnClick="@(async () => await NavigationManager.NavigateToAsync("/controls/labels"))"></Button>
<Button Text="Navigation playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/navigation"))"></Button>
<Button Text="Gesture playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/gestureplayground"))"></Button>
<Button Text="Skia Playground" OnClick="@(async () => await NavigationManager.NavigateToAsync("/skia"))"></Button>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.MobileBlazorBindings.Core/TextSpanContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public string GetUpdatedText(int index, string text)
}
_textSpans[index] = text;

var allText = string.Join(string.Empty, _textSpans);
var allText = string.Concat(_textSpans);
return TrimWhitespace
? allText?.Trim()
? allText.Trim()
: allText;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Microsoft.MobileBlazorBindings.Core;
using System;
using System.Diagnostics;
using MC = Microsoft.Maui.Controls;

namespace Microsoft.MobileBlazorBindings.Elements.Handlers
{
public partial class LabelHandler : ViewHandler, IMauiContainerElementHandler
public partial class LabelHandler : ViewHandler, IMauiContainerElementHandler, IHandleChildContentText
{
private readonly TextSpanContainer _textSpanContainer = new();

public virtual void AddChild(MC.Element child, int physicalSiblingIndex)
{
var childAsSpan = child as MC.Span;
Expand Down Expand Up @@ -47,6 +51,21 @@ public virtual void RemoveChild(MC.Element child)
formattedString.Spans.Remove(childAsSpan);
}

public void HandleText(int index, string text)
{
if (LabelControl.FormattedText != null)
{
if (!string.IsNullOrWhiteSpace(text))
{
throw new InvalidOperationException("Cannot use both string content and Spans for Label.");
}
}
else
{
LabelControl.Text = _textSpanContainer.GetUpdatedText(index, text);
}
}

private MC.FormattedString GetFormattedString()
{
if (LabelControl.FormattedText == null)
Expand Down