了解了swift的语法,对swift搭建界面充满着好奇,终于可以搭建界面了,心中竟然有些许的激动。。。
下面就看下我的第一个swift界面吧
//1.创建一个字典
let colors =
[
"air force blue" : (red:93, green:138, blue:168),
"bittersweet" : (red:254, green:111, blue:94),
"ca" : (red:255, green:140, blue:0),
"air force " : (red:255, green:140, blue:0),
"bitterswee" : (red:143, green:0, blue:255),
"jazf" : (red:113, green:188, blue:120),
"air blue" : (red:228, green:155, blue:15),
"bittersweets" : (red:244, green:0, blue:161),
"jaztt" : (red:252, green:247, blue:94),
"jazbb" : (red:165, green:11, blue:94)
]
//2.创建一个view
let backView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, CGFloat(colors.count * 50)))
backView.backgroundColor = UIColor.blackColor()
var index = 0
//3.(colorName, rgbTuple)这个是元组,用起来超级方便,直接取出字典的key和value
for (colorName, rgbTuple) in colors {
var colorStripe = UILabel(frame: CGRectMake(0.0, CGFloat(index * 50 + 5), 320, 40))
colorStripe.backgroundColor =
UIColor(
red: CGFloat(rgbTuple.red) / 255.0,
green: CGFloat(rgbTuple.green) / 255.0,
blue: CGFloat(rgbTuple.blue) / 255.0,
alpha: 1.0
)
//4.创建label,目的是联系代码
var colorLABEL = UILabel(frame: CGRectMake( 0.0 , 0.0, 300, 40))
colorLABEL.font = UIFont(name: "Arial", size: 20)
colorLABEL.textColor = UIColor.blackColor()
colorLABEL.text = colorName;
colorStripe.addSubview(colorLABEL)
backView.addSubview(colorStripe)
index += 1
}
运行结果:
在驶向swift的路上继续努力。