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

Как вы используете setTitleTextAttributes: forState в UIBarItem?

Как вы используете setTitleTextAttributes:forState: в UIBarItem в iOS?

Как вы устанавливаете NSDictionary? Не могу заставить его работать, и документация не очень понятна.

Из документации:

setTitleTextAttributes:forState:

Устанавливает атрибуты текста заголовков для заданного состояния управления:

- (void)setTitleTextAttributes:(NSDictionary *)attributes 
                      forState:(UIControlState)state

Параметры:

: словарь, содержащий пары ключ-значение для текстовых атрибутов. Вы можете указать шрифт, цвет текста, цвет текста и теневое смещение текста, используя клавиши, перечисленные в NSString UIKit Reference.

state: состояние управления, для которого вы хотите установить текстовые атрибуты для заголовка.

4b9b3361

Ответ 1

Пример кода:

    [[UIBarItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor, 
      [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor, 
      [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
      [UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont, 
      nil] 
     forState:UIControlStateNormal];

Ответ 2

Swift 5.0:

// Bar title text color
let shadow = NSShadow()
shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
shadow.shadowOffset = CGSize(width: 0, height: 1)
let color : UIColor = UIColor(red: 220.0/255.0, green: 104.0/255.0, blue: 1.0/255.0, alpha: 1.0)
let titleFont : UIFont = UIFont(name: "AmericanTypewriter", size: 16.0)!

let attributes = [
    NSAttributedString.Key.foregroundColor : color,
    NSAttributedString.Key.shadow : shadow,
    NSAttributedString.Key.font : titleFont
]
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)

// Or you can use
UIBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)

Swift 4.0:

// Bar title text color
let shadow = NSShadow()
shadow.shadowColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
shadow.shadowOffset = CGSize(width: 0, height: 1)
let color : UIColor = UIColor(red: 220.0/255.0, green: 104.0/255.0, blue: 1.0/255.0, alpha: 1.0)
let titleFont : UIFont = UIFont(name: "AmericanTypewriter", size: 16.0)!

let attributes = [
        NSAttributedStringKey.foregroundColor : color,
        NSAttributedStringKey.shadow : shadow,
        NSAttributedStringKey.font : titleFont
    ]

self.navigationItem.rightBarButtonItem?.setTitleTextAttributes(attributes, for: UIControlState.normal)
// Or you can use
UIBarItem.appearance().setTitleTextAttributes(attributes, for: UIControlState.normal)

Код цели C:

NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[shadow setShadowOffset:CGSizeMake(0, 1)];

NSDictionary *attributes = @{
                                NSForegroundColorAttributeName: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
                                NSShadowAttributeName: shadow,
                                NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:16.0]
                             };

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:attributes forState: UIControlStateNormal];

// Or you can use.

[[UIBarItem appearance] setTitleTextAttributes:attributes forState: UIControlStateNormal];

Ответ 3

Вот код phix23, просто с обновленным, и я думаю, что чище, синтаксис:

[[UIBarItem appearance] setTitleTextAttributes:@{
                      UITextAttributeTextColor: [UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0],
                UITextAttributeTextShadowColor: [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0],
               UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
                           UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:0.0]}
                                      forState: UIControlStateNormal];

Ответ 4

[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor whiteColor], nil] forKeys:[NSArray arrayWithObjects:UITextAttributeTextColor, nil]] forState:UIControlStateNormal];