[Swift 1] Day 3 - Code Exercise Solution


#21
  1. Did you encounter any deal breaking issues? No

  2. Do you have an error that you can’t figure out? So far, so good, thanks for the clear and very detailed explanation

  3. Some comments/questions: I did have a question if you covered this topic about importing XCPlayground, I am also going to try it out. While searching for UIlabel and playground , I came across this module, supposedly this will help or improve the Playground performance…

My code on the exercises…
// declare 2nd image
var image2 = UIImage(named: “mina2.jpg”)
var imageView2 = UIImageView(image: image2)

// changed background to red
eyeColor = UIColor.redColor()
view.backgroundColor = eyeColor

//add seond image to view
view.addSubview(imageView2)

//add label at the bottom of the view
var bottomLabel = UILabel(frame: CGRect(x: 20, y: 420, width: 200, height: 20))
bottomLabel.text = "Hello Playground"
view.addSubview(bottomLabel)


#22

My solution


#23

//: Playground - noun: a place where people can play

import UIKit

var str = "Hi, Paul!"
str
println(“Hi, Paul!”)

var eyeColor = UIColor.blueColor()

for i in 1…100 {
i
}

// iPhone Screen (iPhone 3g)
var view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 400))

// lines of code in iPhone app project
var imageChrysanthemum = UIImage(named: “Chrysanthemum.jpg”)
view.backgroundColor = eyeColor

var imageViewChrysanthemum = UIImageView(image: imageChrysanthemum )
imageViewChrysanthemum.frame = CGRect(x: 0, y: 0, width: 160, height: 120)
//imageViewChrysanthemum.clipsToBounds = false
view.addSubview(imageViewChrysanthemum)

var point = CGPoint(x: 3200.50, y: 4000.33) // centered, one third of the way down
imageViewChrysanthemum.center = point

// Show the view updated
view // playground only

var imageChurchill = UIImage(named: “Churchill.jpg”)
var imageViewChurchill = UIImageView(image: imageChurchill)
imageViewChurchill.frame = CGRect(x: 0, y: 0, width: 160, height: 160)
view.addSubview(imageViewChurchill)

var point2 = CGPoint(x: 3200.50, y: 4000.75) // centered, three fourths of the way down
imageViewChurchill.center = point2
view

// adding label to view

// create label
let labelTitle = UILabel(frame: CGRectMake(0, 0, 150, 25))
// set label properties
labelTitle.text = "My Cat & Hero"
labelTitle.textColor = UIColor.redColor()
labelTitle.backgroundColor = UIColor.whiteColor()
labelTitle.textAlignment = .Center
// calculate position of label
var pointLabel = CGPoint(x: 3200.50, y: 4000.10) // centered, ten percent down
// attach label to view
view.addSubview(labelTitle)
labelTitle.center = pointLabel
view


#24

This is my code!

var image2 = UIImage(named: "min.jpg")
var imageView2 = UIImageView(image: image2)
view.addSubview(imageView2)
var point2 = CGPoint(x: 160, y: 300)
imageView2.center = point2

var label = UILabel()
label.text = "Hello world!"
label.textColor = UIColor.whiteColor()
label.sizeToFit()
view.addSubview(label)
var point3 = CGPoint(x: 160, y: 600)
label.center = point