根据plist文件快速更改标签文本背景颜色

时间:2016-06-02 13:34:55

标签: ios iphone swift

我有一个plist文件,我有一个详细视图,可以读取plist文件并获取相关信息, 我想知道如何检查一个plist项是否具有某个值,如果是,它会将值更改为相应的颜色。

到目前为止我的代码。任何人都不知道我做错了什么并不起作用?

comments

继承人plist文件:

//
//  DetailViewController.swift
//  ConversationalCards
//
//  Created by Avi Tannenbaum on 3/20/16.
//  Copyright © 2016 Avi Tannenbaum. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var modeLabel: UILabel!

    var card: Card?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let card = card {
            navigationItem.title = card.name?.capitalizedString
            imageView.image = UIImage(named: card.name!.lowercaseString)
            titleLabel.text = card.title?.capitalizedString

            //Mode label
            modeLabel.text = card.mode?.capitalizedString
            // Color the text backgrounds and then resize them later for the cards
            //Red

            if ((card.color? = "none") != nil) {
                titleLabel.hidden = false
            }

            if ((card.color? = "0") != nil) {
                titleLabel.backgroundColor = UIColor.greenColor()
            }
            //Green
            else if ((card.color? = "1") != nil) {
             titleLabel.backgroundColor = UIColor.greenColor()
            }
            // Yellow
            else if ((card.color? = "2") != nil) {
              titleLabel.backgroundColor = UIColor.yellowColor()
            }
            // Blue
            else if ((card.color? = "3") != nil) {
              titleLabel.backgroundColor = UIColor.blueColor()
            }

            // lIght green
            else if ((card.color? = "4") != nil) {
               titleLabel.backgroundColor = UIColor(red: 238.0, green: 204, blue: 204.0, alpha: 1.0)

            }
            // darkred Hex code: #C84D59
            else if ((card.color? = "5") != nil) {
              titleLabel.backgroundColor = UIColor(red: 238.0, green: 204.0, blue: 204.0, alpha: 10)
            }
            else if ((card.color? = "6") != nil) {
                titleLabel.backgroundColor = UIColor(red: 104.0, green: 88.0, blue: 139.0, alpha: 1.0)
            }
           // Label style
           // self.titleLabelStyle() 
        } 
    }

    /* not working yet this is the function responseble for converting hex values to UIColor values - Avi Tannenbaum
    extension UIColor {
        public convenience init?(hexString: String) {
            let r, g, b, a: CGFloat

            if hexString.hasPrefix("#") {
                let start = hexString.startIndex.advancedBy(1)
                let hexColor = hexString.substringFromIndex(start)

                if hexColor.characters.count == 8 {
                    let scanner = NSScanner(string: hexColor)
                    var hexNumber: UInt64 = 0
                    if scanner.scanHexLongLong(&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
        }

    } */


    // Style functions
    /*
    func titleLabelStyle() {
        let title = self.titleLabel
        title.numberOfLines = 0
        title.frame = CGRect(x: 10, y: 10, width: 300, height: title.intrinsicContentSize().height)
    }
 */
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

我想要做的是将标签背景设置为相关plist中提到的颜色。

3 个答案:

答案 0 :(得分:2)

Upvoting另一个答案,因为我没有注意到你没有在你的if中进行比较。但是也想补充一点,你可能想在这里考虑一个switch语句而不是一系列if / else语句。例如,您的viewDidLoad可以写成:

override func viewDidLoad() {
    super.viewDidLoad()

    if let card = card {
        navigationItem.title = card.name?.capitalizedString
        imageView.image = UIImage(named: card.name!.lowercaseString)
        titleLabel.text = card.title?.capitalizedString

        //Mode label
        modeLabel.text = card.mode?.capitalizedString

        // Color the text backgrounds and then resize them later for the cards
        //Red

        switch card.color ?? "none" {   // default to none
            case "0":    titleLabel.backgroundColor = UIColor.greenColor()
            case "1":    titleLabel.backgroundColor = UIColor.greenColor()
            case "2":    titleLabel.backgroundColor = UIColor.yellowColor()
            case "3":    titleLabel.backgroundColor = UIColor.blueColor()
            case "4":    titleLabel.backgroundColor = UIColor(red: 238.0, green: 204, blue: 204.0, alpha: 1.0)
            case "5":    titleLabel.backgroundColor = UIColor(red: 238.0, green: 204.0, blue: 204.0, alpha: 1.0)
            case "6":    titleLabel.backgroundColor = UIColor(red: 104.0, green: 88.0, blue: 139.0, alpha: 1.0)
            default:     titleLabel.hidden = false
        }

        // Label style
        // self.titleLabelStyle()

    }
}

答案 1 :(得分:1)

更改if ((card.color? = "none") != nil) {}

到此:

if let colorString = card.color{
    if colorString == "none" {

    }
}

希望它有用: - )

答案 2 :(得分:0)

  1. 您使用了“=”,这是设置您的对象值而不是检查它。你需要使用“==”。
  2. !=零部分是不必要的