【问题标题】:How to convert from UIColor to string and string to UIColor in Xamarin.Ios using c#如何使用 c# 在 Xamarin.Ios 中将 UIColor 转换为字符串和字符串转换为 UIColor
【发布时间】:2016-12-15 10:35:02
【问题描述】:
UILabel label = new UILabel();

 label.TextColor = UIColor.Black;

如何以字符串格式存储 UILabel 颜色?我想从数据库中获取该颜色并应用于 UILabel。如何使用 c# 将 UIColor 转换为字符串和字符串转换为 UIColor?

提前致谢!

【问题讨论】:

    标签: c# ios uicolor


    【解决方案1】:

    首先将颜色存储在 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 个子字符串以获得红、绿、蓝颜色代码。

    【讨论】:

      【解决方案2】:

      可以使用8char的十六进制颜色格式(如#ff0a11ff)

      当您加载/保存字符串时,只需将其解析为字节数组或整数数组,因为 UILabel.FromRGBA 允许您从中创建颜色:https://developer.xamarin.com/api/member/MonoTouch.UIKit.UIColor.FromRGBA/p/System.Int32/System.Int32/System.Int32/System.Int32/

      (很抱歉给出一个anwser而不是评论,我没有足够的声誉)

      【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2019-12-31
      • 2012-08-06
      • 1970-01-01
      • 2011-03-01
      • 2019-06-15
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      相关资源
      最近更新 更多