ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let offsetY : CGFloat = 100
let labelCircleX : CGFloat = 100
let labelX : CGFloat = 200
let colorArray = [UIColor.orangeColor(), UIColor.blueColor(), UIColor.redColor()]
let stringArray = ["Apple", "蘋果", "tv"]
//Strings can include Unicode chacters.
//The Apple logo character can be typed using [option]+[shift]+[K]
//Unfortunately, the Apple logo character cannot be seen in windows.
//Unfortunately, the Apple logo character cannot be seen in windows.
for item in 0...2 {
//Convert Int item to CGFloat using CGFloat(item).
let labelCircleY : CGFloat = offsetY*CGFloat(item+1)
let labelY : CGFloat = labelCircleY + 20
createCharacterInCircle(labelCircleX, y: labelCircleY, text: stringArray[item], color: colorArray[item])
createLabel(labelX, y: labelY, text: stringArray[item])
}
}
func createCharacterInCircle(x: CGFloat, y: CGFloat, text: String, color: UIColor) {
//Get the first character of string "text" and convert it to string.
//"text" can be a string with Unicode characters.
let firstChar = "\(text.characters.first!)" //Swift 2.1
//let firstChar = "\(Array(text)[0])" //Swift 1.2
let labelCircleSize : CGFloat = 70
let labelCircle = UILabel(frame: CGRectMake(x, y, labelCircleSize, labelCircleSize))
//Color Settings
labelCircle.backgroundColor = color
labelCircle.textColor = UIColor.whiteColor()
//Text Settings
labelCircle.text = firstChar
labelCircle.font = UIFont(name: ".HelveticaNeueInterface-Bold", size: 30)
labelCircle.textAlignment = NSTextAlignment.Center
//Circle Settings
labelCircle.layer.cornerRadius = labelCircleSize/2
labelCircle.layer.masksToBounds = true
//Border Settings
labelCircle.layer.borderColor = UIColor.blackColor().CGColor
labelCircle.layer.borderWidth = 3
view.addSubview(labelCircle)
}
func createLabel(x: CGFloat, y: CGFloat, text: String) {
let label = UILabel(frame: CGRectMake(x, y, 100, 25))
label.text = text
view.addSubview(label)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Result:
No comments:
Post a Comment