简体   繁体   中英

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?

You can use Label.TextTransform with TextTransform.Uppercase .

XAML

<Label TextTransform="Uppercase" />

C#

var label = new Label
{
    TextTransform = TextTransform.Uppercase
};

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}}"

Or you can use Bindable property then format the text on the getter : eg:

  public static readonly BindableProperty ItemLabelProperty = 
  BindableProperty.Create(nameof(ItemLabel), typeof(string), 
  typeof(DetailsLineItemControl), default(string), BindingMode.OneWay);
    public string ItemLabel
    {
        get
        {

            var value = (string)GetValue(ItemLabelProperty);
            return !string.IsNullOrEmpty(value) ? value.ToUpper() : value;
        }
        set
        {
            SetValue(ItemLabelProperty, value);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM