UIButton文字列左表示

1573 ワード

  • UIButton
  • を作成する
    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を作成し、座標、タイトル、タイトルの色を設定します.
  • 文字を左にするUILabel文字を左にする書き方、UIButtonはこのように書くべきです:
  •  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;```