簡體   English   中英

中心頂部標簽iOS Objective-C

[英]Center Top Label iOS Objective-C

這是我作為iOS開發人員的新工作的第二天。 到目前為止,我真的很喜歡編寫Objective-C。 如何使我的1行標簽在父級中居中,同時在頂部居中?

到目前為止,我所做的是:

  1. 了解了如何應用字體大小和字體顏色。
  2. 學會了以編程方式創建ui。
  3. 了解到向ui添加標簽。
  4. 使標簽居中,如在父級中心。
  5. 將標簽numberOfLines設置為0。

這是我的代碼:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // loading the viewcontroller, load the controllers/objects.

    // set background and title of the view
    [self.view setBackgroundColor:RGB(19,181,234)];
    self.title = @"UI Main";

    // add the label1
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 40)];
    [myLabel setBackgroundColor:[UIColor clearColor]];
    myLabel.textColor = RGB(255,255,255);
    [myLabel setText:@"Enter Amount"];
    myLabel.numberOfLines = 0;
    [myLabel setFont:[UIFont fontWithName:@"DejaVu Sans" size:10]];
    [myLabel sizeToFit];
    [[self view] addSubview:myLabel];
    // [myLabel release];


}

這是我的模擬器的屏幕截圖: http : //i.stack.imgur.com/AN2JX.png

編輯:我的問題是關於獲取父視圖的大小,以便我可以居中控件。 但是已經回答了! 謝謝!

如果您的Label具有self.view的超級視圖,或者您希望標簽位於self.view(UIView)的中心,請更改此設置

int WidthOfLabel = 100;
int someHeight = 50;
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width/2)-(widthOfLabel/2), 80, widthOfLabel, someHeight)];
myLabel.textAlignment = NSTextAlignmentCenter;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM