Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display label text in uppercase using xaml in Xamarin.Forms

I have an username label and need to view this as uppercase but this should only relate to the UI. The data (string) should be saved in the db as actual case whatever it is. Could anyone tell me if there is anyway to convert it to uppercase without doing so through the code behind?

like image 335
Riyas Avatar asked Aug 29 '17 10:08

Riyas


People also ask

What is StackLayout in Xamarin forms?

StackLayout organizes views in a one-dimensional line ("stack"), either horizontally or vertically. Views in a StackLayout can be sized based on the space in the layout, using layout options.

What is LineBreakMode in Xamarin forms?

If you have a text that is to large to fit the width of your label, you can use the attribute called LineBreakMode=TailTruncation. This will display a text with “…” at the end (ellipsis) when the text doesn't fit. Let's take for example this text: “Welcome to Xamarin Forms” inside a Label like in the exmple below: <?


2 Answers

You can use Label.TextTransform with TextTransform.Uppercase.

XAML

<Label TextTransform="Uppercase" />

C#

var label = new Label
{
    TextTransform = TextTransform.Uppercase
};
like image 53
LeoSouza Avatar answered Sep 20 '22 12:09

LeoSouza


As you're aware you can do this from the code behind as such:

string data = "my data";
UILabel myLabel = new UILabel();
myLabel.Text = data.ToUpper();

So bearing in mind that you don't want to do it this way you would need to derive from UILabel and create your own, then simply add the ToUpper() onto the end of the get;set; values of the Text property.

using CoreGraphics;
using System;
using UIKit;

namespace MyApp.Controls
{
    partial class Control_UpperLabel : UILabel
    {
        public Control_UpperLabel IntPtr handle) : base(handle)
        {
               //
        }

        public Control_UpperLabel()
        {
               //
        }

        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
        }

        public override string Text { get => base.Text.ToUpper(); set => base.Text = value.ToUpper(); }    
   }
}

EDIT: As per comments below, here is an alternative solution for Xamarin.Forms

This uses a value converter as part of a binding solution. It's also been slightly amended to use the suggestion by clint in the comments below. Thanks.

public class StringCaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch ((parameter as string).ToUpper()[0]) 
        { 
        case 'U': 
            return ((string)value).ToUpper(); 
        case 'L': 
            return ((string)value).ToLower(); 
        default: 
            return ((string)value);
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

It would be used in the XAML as such:

Text="{Binding Text, Converter={StaticResource caseConverter}, ConverterParameter=u}}"
like image 20
JoeTomks Avatar answered Sep 20 '22 12:09

JoeTomks