① Label
② Button
-
Style
①
Font : 64
Alignment : center
Background : red
Color : white -
Constraints 制約
①
Horizontally
Vertically
Width : 200
Height : 200
②
Horizontally margin-top : 20
- ViewController.swift
/OmikujiApp/ViewControll.swift
@IBOutlet weak var myLabel: UILabel! // ①Label
@IBAction func getOmikuji(_ sender: Any) { // ②Button
// おみくじの配列
let results = ["大吉", "吉", "中吉", "末吉", "凶", "大凶"]
// 0 ~ n の乱数生成 ⇒ arc4random_uniform(n + 1) ※引数は UInt32 型
let random = arc4random_uniform(UInt32(results.count))
// resultsのrandom番目を myLabel に代入
self.myLabel.text = results[Int(random)]
}
// View が表示されたあとの処理
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 枠線を myLabel に適用
myLabel.layer.borderColor = UIColor.black.cgColor // 枠線の色を指定
myLabel.layer.borderWidth = 5 // 枠線の幅を指定
myLabel.layer.cornerRadius = 50 // 枠線の角の丸みを指定
myLabel.layer.masksToBounds = true // 枠線の外部分の切り取り
myLabel.layer.cornerRadius = myLabel.bounds.width / 2 // 正円の指定
}
Command + =
: テキストと部品を同サイズにするCommand + Option + 0
: Utility Areaの表示非表示切り替えCommand + R
: シュミレータの起動