2013年9月5日木曜日

UIButton : ボタンのタイトルを左寄せにする

UIButtonのタイトルを左寄せにするには、contentHorizontalAlignmentにUIControlContentHorizontalAlignmentLeftを設定する。
タイトルに左からのマージン幅を設定する場合は、titleEdgeInsetsを使用する。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor]
                 forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:16];
[button setFrame:CGRectMake(10, 10, 200, 36)];
[button addTarget:self
               action:@selector(didTapBtn:)
     forControlEvents:UIControlEventTouchUpInside];

// ボタンのタイトルを左寄せにする
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// ボタンのタイトルのマージン設定
// UIEdgeInsetsで(top, left, bottom, right)の順にマージン設定
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 12)];
[self.view addSubview:button];

0 件のコメント:

コメントを投稿