UIButton のタイトルを左揃えに設定するにはどうすればいいですか? 質問する

UIButton のタイトルを左揃えに設定するにはどうすればいいですか? 質問する

の左側に電子メール アドレスを表示する必要がありますUIButtonが、中央に配置されています。

配置を の左側に設定する方法はありますかUIButton?

これが私の現在のコードです:

UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
emailBtn.backgroundColor = [UIColor clearColor];
[emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
[emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailAction:) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:emailBtn];
[emailBtn release];

ベストアンサー1

contentHorizo​​ntalAlignmentを設定します。

// Swift 
emailBtn.contentHorizontalAlignment = .left;

// Objective-C
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

コンテンツの左のインセットを調整することもできます。そうしないと、テキストが左の境界線に触れてしまいます。

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

// Objective-C
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

おすすめ記事