「Swift」UIButtonでボタンのタイトルと色を定義する
書式
button.setTitle(タイトル名, for: .normal)
button.setTitleColor(タイトルの色,for: .normal)
setTitleとsetTitleColorを利用してボタンのタイトルと色を設定します。
使用例
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// ボタンのタイトルを設定
button.setTitle("登録", for: .normal)
// タイトルの色を定義
button.setTitleColor(UIColor.green,for: .normal)
}
}
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// ボタンのタイトルを設定
button.setTitle("登録", for: .normal)
// タイトルの色を定義
button.setTitleColor(UIColor.green,for: .normal)
}
}
import UIKit class ViewController: UIViewController { @IBOutlet weak var button: UIButton! override func viewDidLoad() { super.viewDidLoad() // ボタンのタイトルを設定 button.setTitle("登録", for: .normal) // タイトルの色を定義 button.setTitleColor(UIColor.green,for: .normal) } }
結果
ボタンのタイトルは「登録」、色は緑色になります。