Forum Xamarin.Forms

Grid cutting off Label text

RobertWakeland.4316RobertWakeland.4316 USMember, University ✭✭
edited June 2014 in Xamarin.Forms

I have a settings screen that is made of a grid that alternates between a header row that spans 2 columns and a 2nd row with a description of the setting and the control for that setting, usually a switch. The 2nd row is tall enough to accommodate the switch, but cuts off the text of the Label. I'm assuming I'm missing something in setting up the labels. Here is some sample code:

``
Label lblHeader1 = GetTitleLabel("Header 1");
Label lblText1 = GetTextLabel("Really long text");

        chk1 = new Switch();
        chk1.IsToggled = true;
        chk1.Toggled += chk_Toggled;

        Label lblHeader2 = GetTitleLabel("Header 2");
        Label lblText2 = GetTextLabel("Also really long text.");

        chk2 = new Switch();
        chk2.IsToggled = false;
        chk2.Toggled += chk_Toggled;

        Grid grid = new Grid()
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            RowDefinitions =
            {
                new RowDefinition{Height = new GridLength(1, GridUnitType.Auto)},
                new RowDefinition{Height = new GridLength(1, GridUnitType.Auto)}
            },
            ColumnDefinitions =
            {
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) }
            }
        };

        grid.Children.Add(lblHeader1, 0, 2, 0, 1);
        grid.Children.Add(lblText1, 0, 1);
        grid.Children.Add(chk1, 1, 1);

        grid.Children.Add(lblHeader2, 0, 2, 2, 3);
        grid.Children.Add(lblText2, 0, 3);
        grid.Children.Add(chk2, 1, 3);

``

Here are the helper methods that generate the Labels

``
private Label GetTitleLabel(string text)
{
Label titleLabel = new Label();
titleLabel.Text = text;
titleLabel.Font = Font.SystemFontOfSize(20);
titleLabel.TextColor = Color.FromHex("#000FDE");
titleLabel.LineBreakMode = LineBreakMode.WordWrap;
titleLabel.HorizontalOptions = LayoutOptions.StartAndExpand;

        return titleLabel;
    }

    private Label GetTextLabel(string text)
    {
        Label titleLabel = new Label();
        titleLabel.Text = text;
        titleLabel.Font = Font.SystemFontOfSize(14);
        titleLabel.TextColor = Color.Black;
        titleLabel.LineBreakMode = LineBreakMode.WordWrap;
        titleLabel.HorizontalOptions = LayoutOptions.StartAndExpand;

        return titleLabel;
    }

`

Best Answer

Answers

  • RobertWakeland.4316RobertWakeland.4316 USMember, University ✭✭

    That's not promising. No comment at all? Is this a bug or did I mess up my setup?

  • RobertWakeland.4316RobertWakeland.4316 USMember, University ✭✭

    Good to know. Thanks Daniel.

  • abcr_1abcr_1 Member ✭✭

    Wrap the label with a StackLayout, remove any paddings in the label, and apply those paddings to the wrapping StackLayout instead.

Sign In or Register to comment.