0
votes
UILabel label = new UILabel();

 label.TextColor = UIColor.Black;

How to store the UILabel color in string format? I want to get that color from database and apply to UILabel. How to convert UIColor to string and string to UIColor using c#?

Thanks in advance!

2

2 Answers

2
votes

First store color in DB as UIColor like this

 string color = yourBtn.BackgroundColor.ToString();

Then convert your color to UIColor as

 string hex = color.;
 nfloat red = Convert.ToInt32(string.Format("{0}{0}",hex.Substring(0, 1)), 16) / 255f;
 nfloat green = Convert.ToInt32(string.Format("{0}{0}",hex.Substring(1, 1)), 16) / 255f;
 nfloat blue = Convert.ToInt32(string.Format("{0}{0}",hex.Substring(2, 1)), 16) / 255f;
 UIColor color = UIColor.FromRGB(red, green, blue);

you need to divide string to 3 sub strings to get red,green,blue color codes.

1
votes

You can use the hexadecimal color format of 8char (like #ff0a11ff)

When you load/save the string, simply parse it into a byte array or a int array, as UILabel.FromRGBA allows you to create a color from them : https://developer.xamarin.com/api/member/MonoTouch.UIKit.UIColor.FromRGBA/p/System.Int32/System.Int32/System.Int32/System.Int32/

(sorry to give an anwser and not a comment, I do not have enought reputation)