UIButton文字列左表示
1573 ワード
UIButton *button = [[UIButton alloc] init];
//
button.frame = CGRectMake(100, 100, 100, 50);
//
[button setTitle:@" UIButton" forState:UIControlStateNormal];
//
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//
[button setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:button];
以上のコードはbuttonを作成し、座標、タイトル、タイトルの色を設定します.
button.titleLabel.textAlignment = NSTextAlignmentLeft;```
UIButton 。 , ? UIButton , 。 :
@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment;//how to position content hozontally inside control. default is center typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) { UIControlContentHorizontalAlignmentCenter = 0, UIControlContentHorizontalAlignmentLeft = 1, UIControlContentHorizontalAlignmentRight = 2, UIControlContentHorizontalAlignmentFill = 3, };
contentHorizontalAlignment
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
![button_left.png](http://upload-images.jianshu.io/upload_images/1253942-71eb138d1d40bddb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
UIButton titleEdgeInsets :
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);``` これによりbuttonのtitleは左の10画素からの距離になります.
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;```