簡體   English   中英

UILabel無法在iPhone中的UINavigationBar上居中對齊

[英]UILabel not align center on UINavigationBar in iPhone

我需要在UINavigationBar上對齊UILabel文本中心。 我已經執行了所有步驟,但是執行了一些步驟,使它們不能居中對齊。 下面是代碼。

   UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
   titleLabel.backgroundColor = [UIColor clearColor];
   [titleLabel setTextColor:[UIColor whiteColor]];
   self.titleLabel.text = title;
   self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
   self.titleLabel.adjustsFontSizeToFitWidth = NO;
   self.titleLabel.adjustsLetterSpacingToFitWidth = NO;
   self.titleLabel.textAlignment = NSTextAlignmentCenter;
   [self.titleLabel sizeToFit];
   float titlePadding = 60.0f;
   self.titleLabel.left = titlePadding;
   self.titleLabel.width = self.width - 2 * titlePadding;
   self.titleLabel.top = ceilf(self.frame.size.height / 2 -      self.titleLabel.frame.size.height / 2);

請在這件事上給予我幫助

試試這個會有所幫助

 UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
 label.backgroundColor = [UIColor clearColor];
 label.font = [UIFont boldSystemFontOfSize:20.0];
 label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
 label.textAlignment = NSTextAlignmentCenter;
                     // ^-Use UITextAlignmentCenter for older SDKs.
 label.textColor = [UIColor yellowColor]; // change this color
 self.navigationItem.titleView = label;
 label.text = NSLocalizedString(@"PageThreeTitle", @"");
 [label sizeToFit];

暫無
暫無

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

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