UIButton覚書
- 文字 + 装飾
- 画像
- 画像 (画像はそのままで色かえる)
- 画像 + 文字
※画像はPDF使ってます
/*
文字 + 装飾
*/
let btn1 = UIButton(type: .custom)
btn1.frame = CGRect(x: 50, y: 100, width: 80, height: 40)
// 背景色
btn1.backgroundColor = UIColor.orange
// フォント
btn1.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.0)
// 角丸
btn1.layer.masksToBounds = true
btn1.layer.cornerRadius = 3.0
// 文字と文字色
btn1.setTitle("ボタン", for: .normal)
btn1.setTitleColor(UIColor.white, for: .normal)
btn1.setTitle("ボタン", for: .highlighted)
btn1.setTitleColor(UIColor.gray, for: .highlighted)
self.view.addSubview(btn1)
/*
画像
*/
let btn2 = UIButton(type: .custom)
btn2.frame = CGRect(x: 50, y: 150, width: 40, height: 40)
btn2.setImage(UIImage(named:"trashIcon"), for: .normal)
btn2.setImage(UIImage(named:"trashIcon"), for: .highlighted)
btn2.tintColor = UIColor.purple
self.view.addSubview(btn2)
/*
画像 (画像はそのままで色かえる)
*/
let btn3 = UIButton(type: .custom)
btn3.frame = CGRect(x: 50, y: 200, width: 40, height: 40)
btn3.setImage(UIImage(named:"trashIcon")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn3.setImage(UIImage(named:"trashIcon")?.withRenderingMode(.alwaysTemplate), for: .highlighted)
btn3.tintColor = UIColor.purple
self.view.addSubview(btn3)
/*
画像 + 文字
*/
let btn4 = UIButton(type: .custom)
btn4.backgroundColor = UIColor.lightGray
btn4.frame = CGRect(x: 50, y: 250, width: 120, height: 50)
btn4.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.0)
btn4.setTitle("ゴミ箱", for: .normal)
btn4.setTitleColor(UIColor.white, for: .normal)
btn4.setTitle("ゴミ箱", for: .highlighted)
btn4.setTitleColor(UIColor.gray, for: .highlighted)
btn4.layer.masksToBounds = true
btn4.layer.cornerRadius = 3.0
btn4.setImage(UIImage(named:"trashIcon")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn4.imageView?.contentMode = .scaleAspectFit
btn4.imageView?.tintColor = UIColor.gray
// タイトル位置の調整
btn4.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -50.0, bottom: 0.0, right: 0.0)
// イメージ位置の調整
btn4.imageEdgeInsets = UIEdgeInsets(top: 10.0, left: -20.0, bottom: 10.0, right: 0.0)
self.view.addSubview(btn4)