Подтвердить что ты не робот

Как изменить цвет шрифта/Цвет текста в UIBarButtonItem на панели навигации

Я добавляю кнопку панели в навигационную панель программно следующим образом

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

Теперь я хочу отображать Текст "ОТМЕНА" в RED Color.

Я имею в виду, что мне нужно изменить текст на элементах панели, но не цвет оттенка кнопки.

Как это сделать?

4b9b3361

Ответ 1

Другой способ: -

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

Ответ 2

Проверьте это: -

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

Ответ 3

Просто обновление iOS7 с помощью современного синтаксиса Obj-C:

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

Ответ 4

UITextAttributeTextColor //Is deprecated on iOS 7. 

Этот код используется для изменения цвета текста из прокси-сервера внешнего вида.

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

Ответ 5

этот код используется для изменения цвета текста 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;

Ответ 6

Старый вопрос, вот быстрое решение 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

Ответ 7

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

Ответ 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)