如何解决如何基于json响应值设置UIlabel的backroundColor?
我有一个将UIColor值保存为String的Json数据,并且我想根据这些值更改Label的backgoundColor,但不知道如何做。有什么办法吗?
这是json数据:
{
"id": 1,"title": "black","colorLeft": "000000","colorRight": "000000","type": 1,"leftBrightness": 0.0,"rightBrightness": 0.0
}
这是我的颜色分类:
class ColorCollectionCell: UICollectionViewCell {
@IBOutlet weak var colorName: UILabel!
var colorLeft: String!
var colorRight: String!
var type: Int!
var leftBrightness: Double!
var rightBrightness: Double!
func configureCell(for data: Colors) {
self.colorName.text = data.title
}
func setColor(for data: Colors) {
self.backgroundColor = UIColor.purple
self.colorLeft = data.colorLeft
self.colorRight = data.colorRight
self.type = data.type
self.leftBrightness = data.leftBrightness
self.rightBrightness = data.rightBrightness
uicol
}
}
这是我到目前为止所做的。
解决方法
您可以使用以下行从字符串中设置标签的背景色。
label.backgroundColor = UIColor(named: "your colour name")
根据您的回复:-
colorName.backgroundColor = UIColor(named: self.colorLeft)
,
class ColorCollectionCell: UICollectionViewCell {
@IBOutlet weak var colorName: UILabel!
var colorLeft: String!
var colorRight: String!
var type: Int!
var leftBrightness: Double!
var rightBrightness: Double!
func configureCell(for data: Colors) {
self.colorName.text = data.title
}
func setColor(for data: Colors) {
self.backgroundColor = UIColor.purple
self.colorLeft = data.colorLeft
self.colorRight = data.colorRight
self.type = data.type
self.leftBrightness = data.leftBrightness
self.rightBrightness = data.rightBrightness
let clrLeft = UIColor(hex: "#"+self.colorLeft)!
let clrRight = UIColor(hex: "#"+self.colorRight)!
let gradient = CAGradientLayer()
gradient.frame = colorName.bounds
gradient.colors = [clrLeft.cgColor,clrRight.cgColor]
gradient.startPoint = CGPoint(x: 0.0,y: 0.5)
gradient.endPoint = CGPoint(x: 1.0,y: 0.5)
colorName.layer.insertSublayer(gradient,at: 0)
}
}
extension UIColor {
public convenience init?(hex: String) {
let r,g,b,a: CGFloat
if hex.hasPrefix("#") {
let start = hex.index(hex.startIndex,offsetBy: 1)
let hexColor = String(hex[start...])
if hexColor.count == 8 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexInt64(&hexNumber) {
r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
a = CGFloat(hexNumber & 0x000000ff) / 255
self.init(red: r,green: g,blue: b,alpha: a)
return
}
}
}
return nil
}
}
选中此作为参考。
,我解决了..
extension UIColor {
convenience init(color: String,brightness: String) {
let scanBrightness = Scanner(string: String(describing: brightness))
let scanColor = Scanner(string: color)
scanBrightness.scanLocation = 0
scanColor.scanLocation = 0
var rgbValue: UInt64 = 0
scanBrightness.scanHexInt64(&rgbValue)
scanColor.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(red: CGFloat(r) / 0xff,green: CGFloat(g) / 0xff,blue:
CGFloat(b) / 0xff,alpha: 1)
}
}
在单元格内:
func configureCell(for data: Colors) {
self.colorName.text = data.title
self.type = data.type
self.colorLeft = data.colorLeft
self.colorRight = data.colorRight
self.brightnessLeft = data.leftBrightness
self.brightnessRight = data.rightBrightness
self.rightView.backgroundColor = UIColor.init(color: colorRight,brightness: String(describing: brightnessRight))
self.leftView.backgroundColor = UIColor.init(color: colorLeft,brightness: String(describing: brightnessLeft))
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。