I’ve not mastered AutoLayout, but if you want to resize a label, there’s a few of things you need to do. The image below shows a slider used to control a font size.

 

Resizing a label with a slider

Here I’ve omitted the declarations but fontlabel is a UiLabel, Support.FontSize is a float, and fontSlider is a UISlider. This is the code.

            fontlabel = new UILabel()
            {
                Text = @"Font Size",
                Lines = 1,               
                TextColor = Support.uTextColor,
                BackgroundColor = Support.BackColor,
                LineBreakMode = UILineBreakMode.WordWrap,
                Frame = new RectangleF(10, 320, Frame.Width - 20, 30),
                Font = UIFont.FromName("Helvetica", Support.FontSize)
            };
            fontlabel.SizeToFit();
            AddSubview(fontlabel);
            fontslider = new UISlider(new RectangleF(5, 350, Frame.Width - 5, 30))
            {
                MinValue = 11f,
                MaxValue = 30f,
                Value = Support.FontSize
            };
            fontslider.ValueChanged += HandleFontSliderChanged;

        private void HandleFontSliderChanged(object sender, EventArgs e)
        {
            Support.FontSize = fontslider.Value;
            fontlabel.Font = UIFont.FromName("Helvetica", Support.FontSize);
            fontlabel.SizeToFit();
        }

Allow enough room for the label to fit at its largest size and having changed the font size in the Slider event, call SizeToFit() on the label. Otherwise you’ll find that the text is truncated when the label is made larger.

Leave a Reply

Post Navigation