将字符串转换为 UIColor swift [英] turn string into a UIColor swift

查看:34
本文介绍了将字符串转换为 UIColor swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有填充颜色的按钮:在此处输入图片说明

当用户按下颜色时,会生成一个字符串,即Yellow or Blue or Black etc.

When user press on a color, a string is generated, i.e. Yellow or Blue or Black etc.

我想要的是在我返回时将字符串加载到 UIColor 中:

What i want is to load the string into a UIColor when i segue back:

garageNameLabel.backgroundColor = UIColor."THE STRING THAT WAS GENERATED"

我知道 UIColor.whiteColor(), blackColor() 在那里.

I know that UIColor.whiteColor(), blackColor() is there.

代码:

let theStringColor = Blue

garageNameLabel.backgroundColor = UIColor.theStringColor

谢谢

推荐答案

我通过制作字典解决了问题

I solved the problem by making a dictionary

var colors : [String:UIColor] = ["White": UIColor.whiteColor(), "Black": 
UIColor.blackColor(), "Gray": UIColor.grayColor(),"Turquoise": 
UIColor.cyanColor(),"Red": 
UIColor.redColor(),"Yellow":UIColor.yellowColor(),"Blue": UIColor.blueColor(), 
"Green": UIColor.greenColor()]

然后将字符串加载到其中,即:

and then loading the string into it i.e.:

let theStringColor = "Blue"

garageNameLabel.backgroundColor = colors[theStringColor]

这篇关于将字符串转换为 UIColor swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆