Forum Xamarin.Forms

how to make a label align is justify?

lescperlescper Member ✭✭✭✭✭

Can it be set to justify of the align?

Tagged:

Best Answer

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    @lescper In Xamarin.Forms, the TextAlignment enum doesn't provide the justify type. For the function, you could use a WebView to display the text content.

    var source = new HtmlWebViewSource();
    var text = "<html>" +
            "<body  style=\"text-align: justify;\">" +
            string.Format("<p>{0}</p>", label.Text) +
            "</body>" +
            "</html>";
    source.Html = text;
    browser.Source = source;
    

    Or create a custom Label renderer to achieve this function in each platform.

    Check the code:
    Custom control

    public class CustomLabel : Label
    {
        public static readonly BindableProperty JustifyTextProperty =
            BindableProperty.Create(
                propertyName: nameof(IsJustifiedText),
                returnType: typeof(Boolean),
                declaringType: typeof(CustomLabel),
                defaultValue: false,
                defaultBindingMode: BindingMode.OneWay
         );
    
        public bool IsJustifiedText
        {
            get { return (Boolean)GetValue(JustifyTextProperty); }
            set { SetValue(JustifyTextProperty, value); }
        }
    }
    

    Custom renderer class

    [assembly:ExportRenderer(typeof(CustomLabel),typeof(CustomLabelRenderer))]
    namespace TestApplication_1.Droid
    {
        public class CustomLabelRenderer : LabelRenderer
        {
            public CustomLabelRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
                var el = (Element as CustomLabel);
    
                if (el != null && el.IsJustifiedText)
                {
                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                    {
                        Control.JustificationMode = Android.Text.JustificationMode.InterWord;
                    }
                }
            }
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    Accepted Answer

    @lescper In Xamarin.Forms, the TextAlignment enum doesn't provide the justify type. For the function, you could use a WebView to display the text content.

    var source = new HtmlWebViewSource();
    var text = "<html>" +
            "<body  style=\"text-align: justify;\">" +
            string.Format("<p>{0}</p>", label.Text) +
            "</body>" +
            "</html>";
    source.Html = text;
    browser.Source = source;
    

    Or create a custom Label renderer to achieve this function in each platform.

    Check the code:
    Custom control

    public class CustomLabel : Label
    {
        public static readonly BindableProperty JustifyTextProperty =
            BindableProperty.Create(
                propertyName: nameof(IsJustifiedText),
                returnType: typeof(Boolean),
                declaringType: typeof(CustomLabel),
                defaultValue: false,
                defaultBindingMode: BindingMode.OneWay
         );
    
        public bool IsJustifiedText
        {
            get { return (Boolean)GetValue(JustifyTextProperty); }
            set { SetValue(JustifyTextProperty, value); }
        }
    }
    

    Custom renderer class

    [assembly:ExportRenderer(typeof(CustomLabel),typeof(CustomLabelRenderer))]
    namespace TestApplication_1.Droid
    {
        public class CustomLabelRenderer : LabelRenderer
        {
            public CustomLabelRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
            {
                base.OnElementChanged(e);
                var el = (Element as CustomLabel);
    
                if (el != null && el.IsJustifiedText)
                {
                    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
                    {
                        Control.JustificationMode = Android.Text.JustificationMode.InterWord;
                    }
                }
            }
        }
    }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • JohnHardmanJohnHardman GBUniversity admin

    @Jarvan said:
    @lescper In Xamarin.Forms, the TextAlignment enum doesn't provide the justify type. For the function, you could use a WebView to display the text content.

    Remember though, that using justify can make text hard to read for some people with particular accessibility requirements.

    See https://w3c.github.io/low-vision-a11y-tf/requirements.html#justification

Sign In or Register to comment.