StarShows Studios StarShows Studios - 6 months ago 25
iOS Question

Swift change label text background color according to plist file

I have a plist file and I have a detail view that reads the plist file and gets information pretty well,
I would like to know how I can check if a plist item has a certain value and if it does it will change the value to the according color.

Heres my code so far. it doesn't work anyone know what I'm doing wrong?

//
// 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()
}
}


heres the plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Begin</string>
<key>title</key>
<string> </string>
<key>mode</key>
<string> </string>
<key>color</key>
<string>none</string>
<key>group</key>
<string>Getting Started</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>THE COMPLIMENT
I WANT TO GIVE YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>TALK ABOUT YOUR NEXT VACATION...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK OUR BEST MEMORY IS...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>AN ADVENTURE I WANT TO SHARE WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I TRUST YOU BECAUSE...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FELL IN LOVE WITH YOU WHEN...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A HUG...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>0</string>
<key>group</key>
<string>Date One</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I REALLY APPRECIATE WHEN YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>You MAKE ME REALLY HAPPY WHEN...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I BELIEVE IN YOU BECAUSE...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING SILLY l WANT TO DO WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO PAMPER YOU BY...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A SMALL DOSE OF ROMANCE WOULD INCLUDE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>HOLD HANDS WITH EYES CLOSED...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>1</string>
<key>group</key>
<string>Date Two</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A GIFT I WOULD LIKE TO RECEIVE FROM YOU...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE THING I HAVE LEARNED FROM YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO SHOW YOU SUPPORT BY...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WHAT IS YOUR FAVOURITE ROMANTIC MOVIE? WHY?</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY ATTRACTION TO YOU DEEPENED WHEN...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I Think OUR RELATIONSHIP IS AT ITS BEST WHEN...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Three</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WRITE YOUR PARTNER A THANKYOU NOTE...</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>2</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WAS FIRST ATTRACTED TO YOU BECAUSE...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>You BRING OUT THE BEST IN ME BY...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I&apos;D LIKE TO STUDY _ WITH YOU.</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING CRAZY I WANT TO DO WITH YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE WAY TO TREAT OURSELVES TOGETHER...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK OF US WHEN l HEAR THIS SONG...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Four</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A HEAD MASSAGE.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>3</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY EARLIEST MEMORY OF OUR FRIENDSHIP...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>SOMETHING EXCITING WE CAN DO TOGETHER...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>OUR IDEAL DAY TOGETHER...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>IF I COULD BUY YOU ANYTHING I WOULD BUY YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FEEL CLOSE TO YOU WHEN...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>DESCRIBE A ROMANTIC EVENING</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>WRITE YOUR PARTNER A COMPLIMENT NOTE.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>4</string>
<key>group</key>
<string>Date Five</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>THE TALENT OF YOURS I REALLY LIKE...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I ADMIRE THA T YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT TO THANK YOU FOR...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>A SURPRISE I&apos;D LIKE FROM YOU...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I LOVE YOU BECAUSE...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>MY EARLIEST MEMORY OF OUR ROMANCE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>RECEIVE A SHOULDER MASSAGE</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>5</string>
<key>group</key>
<string>Date SIx</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE OF YOUR STRENGTHS...</string>
<key>mode</key>
<string>WARM UP</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>ONE PLEASURE I WOULD LIKE TO GIVE YOU...</string>
<key>mode</key>
<string>ENCOURAGING EACH OTHER</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>OUR LOVE REMINDS ME OF THIS CANDY...</string>
<key>mode</key>
<string>STRENGTHENING YOUR RELATIONSHIP</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I FEEL SAFE WITH YOU BECAUSE...</string>
<key>mode</key>
<string>A LITTLE BIT OF FUN</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I WANT YOU TO PAMPER ME BY...</string>
<key>mode</key>
<string>DEEPENING YOUR BOND</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>I THINK YOU ARE SEXY BECAUSE...</string>
<key>mode</key>
<string>FUEL YOUR ROMANCE</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
<dict>
<key>name</key>
<string>Card</string>
<key>title</key>
<string>LOOK INTO EACH OTHERS EYES.</string>
<key>mode</key>
<string>TRY THIS</string>
<key>color</key>
<string>6</string>
<key>group</key>
<string>Date Seven</string>
</dict>
</array>
</plist> "


what im trying to do is to set the label background to the the color mentioned in the relevant plist.

Answer

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

to this:

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

    }
}

hope it helpful :-)

Comments