1 UILabels

http://docs.fabo.io/swift/uikit/img/uikit001.png Swift 3.0 // // ViewController.swift // uikit001 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // ボタンのサイズを定義. let bWidth: CGFloat = 200 let bHeight: CGFloat = 50 // 配置する座標を定義(画面の中心). let posX: CGFloat = self.view.bounds.width/2 - bWidth/2 let posY: CGFloat = self.view.bounds.height/2 - bHeight/2 // Labelを作成. let label: UILabel = UILabel(frame: CGRect(x: posX, y: posY, width: bWidth, height: bHeight)) // UILabelの背景をオレンジ色に. label.backgroundColor = UIColor.orange // UILabelの枠を丸くする. label.layer.masksToBounds = true // 丸くするコーナーの半径. label.layer.cornerRadius = 20.0 // 文字の色を白に定義. label.textColor = UIColor.white // UILabelに文字を代入. label.text = "Hello Swift!!" // 文字の影をグレーに定義. label.shadowColor = UIColor.gray // Textを中央寄せにする. label.textAlignment = NSTextAlignment.center // Viewの背景を青にする. self.view.backgroundColor = UIColor.cyan // ViewにLabelを追加. self.view.addSubview(label) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } Swift 2.3 // // ViewController.swift // uikit001 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // ボタンのサイズを定義. let bWidth: CGFloat = 200 let bHeight: CGFloat = 50 // 配置する座標を定義(画面の中心). let posX: CGFloat = self.view.bounds.width/2 - bWidth/2 let posY: CGFloat = self.view.bounds.height/2 - bHeight/2 // Labelを作成. let label: UILabel = UILabel(frame: CGRect(x: posX, y: posY, width: bWidth, height: bHeight)) // UILabelの背景をオレンジ色に. label.backgroundColor = UIColor.orangeColor() // UILabelの枠を丸くする. label.layer.masksToBounds = true // 丸くするコーナーの半径. label.layer.cornerRadius = 20.0 // 文字の色を白に定義. label.textColor = UIColor.whiteColor() // UILabelに文字を代入. label.text = "Hello Swift!!" // 文字の影をグレーに定義. label.shadowColor = UIColor.grayColor() // Textを中央寄せにする. label.textAlignment = NSTextAlignment.Center // Viewの背景を青にする. self.view.backgroundColor = UIColor.cyanColor() // ViewにLabelを追加. self.view.addSubview(label) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 2.xと3.xの差分 UIColor.grayColor()がUIColor.grayに変更 NSTextAlignment.Centerが、NSTextAlignment.centerに変更 Reference UIColor https://developer.apple.com/reference/uikit/uicolor UILabel https://developer.apple.com/reference/uikit/uilabel

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.