1

我有一个UINavigationBar带有 2 行标签的自定义titleView

        UILabel *navBarLabel = [[UILabel alloc] initWithFrame:CGRectZero];  
        UINavigationItem *item = [[UINavigationItem alloc] init];  
        navBarLabel.backgroundColor = [UIColor clearColor];
        navBarLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
        navBarLabel.numberOfLines = 2;
        navBarLabel.textAlignment = UITextAlignmentCenter;
        navBarLabel.textColor = [UIColor colorWithRed:124.0/255.f green:125.0/255.f blue:128.0/255.f alpha:1.0];
        navBarLabel.text = @"This\nis an example";
        [navBarLabel sizeToFit];
        item.titleView = navBarLabel;

但是UILabel,我不想使用 2 行文本来制作 my ,而是添加 2 UILabels一个在另一个之上,以分别实现对每个“行”中字体的自定义。它是如何实施的?有任何想法吗?

4

2 回答 2

7

只需创建一个父容器UIView来保存您的两个标签,并将此父容器用作您的标题视图。在伪代码中:

   UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];
   [titleView addSubview:someLabel];
   [titleView addSubview:anotherLabel];
   item.titleView = titleView;

...您只需要确保您已正确设置标签框架,以便一个标签位于另一个标签之上。你如何做到这一点取决于你是否使用自动布局,但它非常简单明了。

于 2013-01-29T12:14:36.100 回答
-2

iOS 允许您在栏中添加两个项目。试试下面的代码。

  UIButton *tempButton = nil;
    UIImage  *tempButtonImage = nil;
    UIImage  *tempButtonPressedImage = nil;

    tempButtonImage = [UIImage imageNamed:@"unselected1.png"];
    tempButtonPressedImage = [UIImage imageNamed:@"selected1.png"];
    tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setImage : tempButtonImage forState : UIControlStateNormal];
    [tempButton setImage : tempButtonPressedImage forState : UIControlStateHighlighted];
    [tempButton addTarget : self action : @selector(tempButtonClick:) forControlEvents : UIControlEventTouchUpInside];
    tempButton.frame = CGRectMake(0, 0, tempButtonImage.size.width, tempButtonImage.size.height);

    UIView *tempButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, tempButtonImage.size.width, tempButtonImage.size.height}];
    tempButtonContainer.backgroundColor = [UIColor clearColor];
    [tempButtonContainer addSubview:tempButton];

    UIBarButtonItem *tempToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:tempButtonContainer];
    // custom logout button
    UIButton *temp2Button = nil;
    UIImage  *temp2ButtonImage = nil;
    UIImage  *temp2ButtonPressedImage = nil;

    temp2ButtonImage = [UIImage imageNamed:@"unselectedtemp2Button.png"];
    temp2ButtonPressedImage = [UIImage imageNamed:@"selectedtemp2Button.png"];
    temp2Button = [UIButton buttonWithType:UIButtonTypeCustom];
    [temp2Button setImage : temp2ButtonImage forState : UIControlStateNormal];
    [temp2Button setImage : temp2ButtonPressedImage forState : UIControlStateHighlighted];
    [temp2Button addTarget : self action : @selector(temp2ButtonClick:) forControlEvents : UIControlEventTouchUpInside];
    temp2Button.frame = CGRectMake(0, 0, temp2ButtonImage.size.width, temp2ButtonImage.size.height);

    UIView *temp2ButtonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, temp2ButtonImage.size.width, temp2ButtonImage.size.height}];
    temp2ButtonContainer.backgroundColor = [UIColor clearColor];
    [temp2ButtonContainer addSubview:temp2Button];

    UIBarButtonItem *temp2ToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:temp2ButtonContainer];

    // setting right items
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:tempToolbarButton, temp2ToolbarButton, nil];


    UIButton *button = nil;
    UIImage  *buttonImage = nil;
    UIImage  *buttonPressedImage = nil;

    buttonImage = [UIImage imageNamed:@"asd.png"];
    buttonPressedImage = [UIImage imageNamed:@"selected-asd.png"];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage : buttonImage forState : UIControlStateNormal];
    [button setImage : buttonPressedImage forState : UIControlStateHighlighted];
    [button addTarget : self action : @selector(buttonClick:) forControlEvents : UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

    UIView *buttonContainer = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, buttonImage.size.width, buttonImage.size.height}];
    buttonContainer.backgroundColor = [UIColor clearColor];
    [buttonContainer addSubview:button];

    UIBarButtonItem *toolbarButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];

    self.navigationItem.leftBarButtonItem = toolbarButton;
于 2013-01-29T12:33:42.113 回答