【问题标题】:How to change the font color / Text Color of the UIBarButtonItem on navigation bar如何更改导航栏上 UIBarButtonItem 的字体颜色/文本颜色
【发布时间】:2012-08-30 23:51:43
【问题描述】:

我以编程方式将条形按钮添加到导航栏,如下所示

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)];
    self.navigationItem.leftBarButtonItem = cancel;

现在我想用红色显示文本“CANCEL”。

我的意思是我需要更改栏按钮项上的文本,而不是按钮的色调。

怎么做?

【问题讨论】:

标签: iphone objective-c ios xcode ipad


【解决方案1】:

看看这个:-

  UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor],  UITextAttributeTextColor,nil] forState:UIControlStateNormal];

【讨论】:

  • 导致崩溃:线程1:EXC_BAD_ACESS (code=2 address=0x0)
  • +1 完美运行 .thanks .@ user1645721 setTitleTextAttributes 函数可从 IOS 5 获得
  • 需要注意的是UITextAttributeTextColor在7.0中被替换为NSForegroundColor。
  • NSForegroundColorAttributeName 是 UITextAttributeTextColor 的正确替代品
【解决方案2】:

另一种方法是:-

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
 button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete)  forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];

【讨论】:

    【解决方案3】:

    此代码用于更改导航栏上 UIBarButtonItem 的文本颜色:

    UILabel *lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
    lblTotCaratteri.textAlignment = UITextAlignmentCenter;
    lblTotCaratteri.font = [UIFont italicSystemFontOfSize:13.0];
    lblTotCaratteri.textColor = [UIColor redColor];
    lblTotCaratteri.backgroundColor = [UIColor clearColor];
    lblTotCaratteri.adjustsFontSizeToFitWidth = YES;
    lblTotCaratteri.text = @"Cancel";
    
    UIBarButtonItem *lblCaratteri = [[UIBarButtonItem alloc] initWithCustomView: lblTotCaratteri];
    
    self.navigationItem.rightBarButtonItem = lblCaratteri;
    

    【讨论】:

      【解决方案4】:
      UITextAttributeTextColor //Is deprecated on iOS 7. 
      

      此代码用于从外观代理更改文本颜色。

      [[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
      

      【讨论】:

      • 这个设置背景颜色,他要求文本颜色。
      • 使用 NSForegroundColorAttributeName 代替 UITextAttributeTextColor
      【解决方案5】:

      UITextAttributeTextColor //iOS 7 已弃用。

      这样设置 BarButtonItem 的颜色

          [_barButtonItem setTitleTextAttributes:
                          [NSDictionary dictionaryWithObjectsAndKeys: 
                                   [UIColor colorWithRed:250/255.0 
                                                   green:240/255.0 
                                                   blue:230/255.0 
                                                   alpha:1.0],  
                                   NSForegroundColorAttributeName,nil] 
                          forState:UIControlStateNormal];
      

      【讨论】:

        【解决方案6】:

        只是使用现代 Obj-C 语法的 iOS7 更新:

        [barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案7】:

          老问题,这是 swift 2.2 解决方案:

              let cancel = UIBarButtonItem(title: "CANCEL", style: .Bordered, target: self, action: #selector(goToPreviousView))
              cancel.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal)
              self.navigationItem.leftBarButtonItem = cancel
          

          【讨论】:

            【解决方案8】:

            如果不是您的项目,而您只需要添加一些更改,每个人都应该做的主要事情是检查

            [UIBarButtonItem appearance]
            

            我浪费了很多时间才意识到有人设置了错误的 UIBarButtonItem 外观

            【讨论】:

              【解决方案9】:

              这里是更新的swift 4.0版本代码:

              let reset = UIBarButtonItem(title: "Reset All", style: .plain , target: self, action: #selector(self.resetButtonClicked(_ :) ))
              reset.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
              

              【讨论】:

                【解决方案10】:

                斯威夫特 4.2

                let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
                doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
                

                【讨论】:

                  【解决方案11】:

                  斯威夫特 4.2

                  UIBarButtonItem 使用属性文本:

                  func createCancelButton() {
                          guard let font = UIFont(name: "OpenSans", size: 12) else { return }
                          let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
                          cancelButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.blue, NSAttributedString.Key.font : font], for: .normal)
                          navigationItem.leftBarButtonItem = cancelButton
                      }
                  
                      @objc func cancelTapped() {
                          print("cancelTapped")
                      }
                  

                  UIBarButtonItem 使用纯文本:

                  func createCancelButton() {
                          let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
                          cancelButton.tintColor = UIColor.blue
                          navigationItem.leftBarButtonItem = cancelButton
                      }
                  
                      @objc func cancelTapped() {
                          print("cancelTapped")
                      }
                  

                  【讨论】:

                    【解决方案12】:

                    如果您需要设置不同于默认外观的颜色,请使用:

                    barButtonItem.tintColor = .red
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-07-26
                      • 2021-07-23
                      • 2014-07-30
                      • 1970-01-01
                      • 2014-08-16
                      • 2016-11-07
                      相关资源
                      最近更新 更多