LoginSignup
5
4

More than 5 years have passed since last update.

Swift コードからUIButton生成時の設定一覧

Last updated at Posted at 2014-11-07

storyboardではなく処理の中でボタン追加した時などに。

ボタン生成の流れ



//サイズ、配置場所指定してボタン作成
let startButton:UIButton = UIButton(frame: CGRectMake(0, 40, 100, 20))

//ボタン内テキストの文字数がオーバーした時、語尾に"..."を付ける
startButton.titleLabel?.lineBreakMode = NSLineBreakMode.ByTruncatingTail

//ボタン内テキストを左寄せにする
startButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left

//テキストの色設定
startButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

//ボタン内のテキスト
startButton.setTitle("スタート", forState: UIControlState.Normal)

//Viewに追加
self.view.addSubview(startButton)

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4