Swift - 21 - 字典实战和UIKit初探
//: Playground - noun: a place where people can play import UIKit // 数据源 let colors = [ "Air Force Blue":(red:93, green:138, blue:168), "Bittersweet":(red:254, green:111, blue:94), "Canary Yellow":(red:255, green:239, blue:0), "Dark Orange":(red:255, green:140, blue:0), "Electric Violet":(red:255, green:140, blue:0), "Fern":(red:113, green:188, blue:120), "Gamboge":(red:228, green:155, blue:15), "Hollywood Cerise":(red:244, green:0, blue:161), "Icterine":(red:252, green:247, blue:94), "Jazzberry Jam":(red:165, green:11, blue:94), ] // 创建一个UIView用于显示 var rect = CGRectMake(0, 0, 320, (CGFloat)(colors.count * 50)); var backView = UIView(frame:rect) backView.backgroundColor = UIColor.blackColor() backView var index = 0 for (colorName, rgbTuple) in colors { // 创建UILabel用来显示颜色, 并将其放在view上 var labelRect = CGRectMake(0, (CGFloat)(index * 50 + 5), 320, 40) var colorStripe = UILabel(frame: labelRect) colorStripe.text = colorName colorStripe.backgroundColor = UIColor( red: (CGFloat)(rgbTuple.red) / 255.0, green: (CGFloat)(rgbTuple.green) / 255.0, blue: (CGFloat)(rgbTuple.blue) / 255.0, alpha: 1.0 ) colorStripe backView.addSubview(colorStripe) index++ } backView