0
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!

Deepak
  • 545
  • 1
  • 12
  • 36

2 Answers2

2

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.

Mounika
  • 412
  • 2
  • 20
1

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)

Greg
  • 579
  • 5
  • 26
  • 1
    Correct, a good stackoverflow post about that http://stackoverflow.com/questions/10310917/uicolor-from-hex-in-monotouch – OrcusZ Dec 15 '16 at 10:54