UILabel未在iPhone中的UINavigationBar上对齐中心

时间:2013-11-15 23:31:35

标签: iphone sdk uilabel uinavigationbar

我需要在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);

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

试试这会有所帮助

 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];