【发布时间】:2016-12-15 10:35:02
【问题描述】:
UILabel label = new UILabel();
label.TextColor = UIColor.Black;
如何以字符串格式存储 UILabel 颜色?我想从数据库中获取该颜色并应用于 UILabel。如何使用 c# 将 UIColor 转换为字符串和字符串转换为 UIColor?
提前致谢!
【问题讨论】:
UILabel label = new UILabel();
label.TextColor = UIColor.Black;
如何以字符串格式存储 UILabel 颜色?我想从数据库中获取该颜色并应用于 UILabel。如何使用 c# 将 UIColor 转换为字符串和字符串转换为 UIColor?
提前致谢!
【问题讨论】:
首先将颜色存储在 DB 中作为 UIColor 像这样
string color = yourBtn.BackgroundColor.ToString();
然后将您的颜色转换为 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);
您需要将字符串分成 3 个子字符串以获得红、绿、蓝颜色代码。
【讨论】:
可以使用8char的十六进制颜色格式(如#ff0a11ff)
当您加载/保存字符串时,只需将其解析为字节数组或整数数组,因为 UILabel.FromRGBA 允许您从中创建颜色:https://developer.xamarin.com/api/member/MonoTouch.UIKit.UIColor.FromRGBA/p/System.Int32/System.Int32/System.Int32/System.Int32/
(很抱歉给出一个anwser而不是评论,我没有足够的声誉)
【讨论】: