Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

AutoCalcul size of a UILabel

$
0
0

Hey all,

I'm developping an application that get description form a server. this description is only a string and can be very long. So sometimes I need to scroll to see all the description. But I meet a problem to calculate the height size.

Here is my code

`

        UILabel description = new UILabel();
        description.TextColor = UIColor.White;
        description.BackgroundColor = UIColor.Green;
        description.Font = UIFont.FromName("Helvetica", 14f);
        description.LineBreakMode = UILineBreakMode.WordWrap;

        description.TextAlignment = UITextAlignment.Justified;
        string descriptionText = news.Description.TrimStart('\n').TrimEnd('\n');
        description.Text = descriptionText;
        description.Frame = new RectangleF(10, 110, 300, ((descriptionText.Length / 40) * 20) );
        description.Lines = int.Parse((descriptionText.Length / 40).ToString()) + 1;

        scrollView = new UIScrollView
        {
            Frame = new RectangleF(0, 0, View.Frame.Width, View.Frame.Height),
            ContentSize = new SizeF(View.Frame.Width, containerHeaderNews.Frame.Height + description.Frame.Height),
            AutoresizingMask = UIViewAutoresizing.FlexibleHeight
        };
        scrollView.AddSubview(containerHeaderNews);
        scrollView.AddSubview(description);
        View.AddSubview(scrollView);

`

So I try to calculate the number of possible line and the possible size of my label. But I think it's not the best solution. Cause when I've got a lot of text, the size is too big.

thank you


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>