15

我正在使用 UINavigationItem 的 titleView 属性来设置具有我想要的字体大小/颜色的自定义 UILabel。这是我的代码:

self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;

在导航栏中,我还有一个左栏按钮。结果是:文本没有正确居中。我试过设置标签的 x 原点,但这没有效果。

4

7 回答 7

19

而不是initWithFrame只使用init并放在最后一行代码[self.headerLabel sizeToFit] 之后。

于 2011-10-26T11:47:24.113 回答
15

如果将 headerLabel 设置为 titleView 的子视图,则可以设置 headerLabel 的框架来控制它在 titleView 中的位置。

你现在这样做的方式,你没有那种控制权。我认为操作系统会根据可用空间为您选择 titleView 的框架。

希望这可以帮助!

于 2010-06-26T19:41:24.103 回答
3

我在应用商店中的每个应用中都为导航栏使用了自定义标题标签。我已经测试了许多不同的方法,到目前为止,在导航栏中使用自定义标签的最简单方法是完全忽略titleView并将标签直接插入到navigationController.view.

使用这种方法,很容易让标题标签的框架始终与导航栏的框架匹配——即使您使用的是非标准尺寸的自定义导航栏。

- (void)viewDidLoad {
   [self.navigationController.view addSubview:self.titleLabel];
   [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
             (UIInterfaceOrientation)interfaceOrientation {
   return YES;
}

- (void)didRotateFromInterfaceOrientation:
             (UIInterfaceOrientation)fromInterfaceOrientation {
   [self frameTitleLabel];
}

- (UILabel *) titleLabel {
   if (!titleLabel) {
      titleLabel = [[UILabel alloc]           
          initWithFrame:self.navigationController.navigationBar.frame];

       titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
       titleLabel.text = NSLocalizedString(@"Custom Title", nil);
       titleLabel.textAlignment = UITextAlignmentCenter;
       titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
   }
   return titleLabel;
}

- (void) frameTitleLabel {
   self.titleLabel.frame = self.navigationController.navigationBar.frame;
}

这种方法的一个警告是,如果您不小心并将标题设置得太长,您的标题可能会溢出导航栏中的任何按钮的顶部。但是,IMO,与 1) 当你有一个 rightBarButton 时标题没有正确居中或 2) 如果你有一个 leftBarButton 标题没有出现,那么处理起来的问题要小得多。

于 2012-03-01T21:45:30.160 回答
3

我有同样的问题;我只是通过计算标题长度并相应地设置标签框架宽度以某种方式解决了这个问题。虽然这不是一个完美的,但可以管理。这是代码。

label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [ UIFont fontWithName: @"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor orangeColor];
//label.text=categoryTitle;
CGFloat verticalOffset = 2; 
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)           
{
    if (categoryTitle.length > 8)
    {
    label.frame = CGRectMake(0, 0, 300, 44);
    }else {
        label.frame = CGRectMake(0, 0, 80, 44);
    }

    self.navigationItem.titleView = label;  
     self.navigationItem.title=label.text;
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset   forBarMetrics:UIBarMetricsDefault];
     [[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];


}
于 2012-09-11T04:39:08.343 回答
1

只需计算所需的确切帧大小并左对齐:

UIFont* font = [UIFont fontWithName:@"Bitsumishi" size:20];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap]; 
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.font = font;
label.textAlignment = UITextAlignmentLeft;
label.text = title;
self.titleView = label;
于 2011-04-02T18:12:03.517 回答
0

对我有用的是在 viewDidAppear 方法中更新 titleView 框架。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView *titleView = self.navigationItem.titleView;
    CGRect navBarFrame = self.navigationController.navigationBar.frame;
    [titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth) / 2, (CGRectGetHeight(navBarFrame) - TitleHeight) / 2, TitleWidth, TitleHeight)];
}
于 2012-11-21T19:25:10.777 回答
0
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
lbl.text = @"Home";
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
lbl.shadowColor = [UIColor blackColor];
lbl.shadowOffset = CGSizeMake(0,1);
self.navigationItem.titleView = vw;
[self.navigationItem.titleView addSubview:lbl];
于 2011-10-18T05:53:19.407 回答