0

I need a label to show a number in the range of thousands. I'm not using thousand separator as per requested, but I want the user to be able to focus on the thousands. For example if I were to show "14013", I want the "14" to be shown in a bigger font size than "013". I'm using a workaround of turning the number into string and using two separate label, but is there any string format I can actually use to make this into just one label?

1 Answer 1

2

Updates

You could use Binding value converters together with Span.

I made a demo for the case.

Here is MyConverter class. For example, if you pass a string like 12345, the converter will return 12 or 345 according to the Parameter passed to it.

public class MyConverter : IValueConverter
{
    public MyConverter()
    {
    }

    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var str = value as string;
        if((string)parameter == "thousand")
        {
            return str.Substring(0,str.Length - 3);
        }

        return str.Substring(str.Length - 3);
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Then consume it in XAML,

<ContentPage.Resources>
        <local:MyConverter x:Key="myconverter"/>
</ContentPage.Resources>

......

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{Binding MyText,Converter={StaticResource Key=myconverter},ConverterParameter=thousand}"
                  TextColor="Red" FontAttributes="Bold" FontSize="20"/>
            <Span Text="{Binding MyText,Converter={StaticResource Key=myconverter}}"

                  FontSize="14"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

In this way, you don't have to separate into two strings in the ViewModel.


Here is the screenshot

enter image description here

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

I am using MVVM Binding. So in the end I still do have to separate the value into two strings. Thank you for the answer.
@DeanThomas if you don't want to separate the value, I recommend you using Binding value converters. Please check my updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.