你如何在UIBarItem中使用setTitleTextAttributes:forState?

BeM*_*ase 36 objective-c uinavigationcontroller ios uibaritem swift

如何使用setTitleTextAttributes:forState:UIBarItemiOS

你怎么设置的NSDictionary?不能使它工作,文档不是很清楚.

从文档:

setTitleTextAttributes:forState:
Run Code Online (Sandbox Code Playgroud)

设置给定控件状态的标题文本属性:

- (void)setTitleTextAttributes:(NSDictionary *)attributes 
                      forState:(UIControlState)state
Run Code Online (Sandbox Code Playgroud)

参数:

attributes:包含文本属性的键值对的字典.您可以使用NSString UIKit Additions Reference中列出的键指定字体,文本颜色,文本阴影颜色和文本阴影偏移.

state:要为其设置标题文本属性的控件状态.

Fel*_*lix 102

示例代码:

    [[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];
Run Code Online (Sandbox Code Playgroud)

  • 从iOS 7开始不推荐使用UITextAttribute,你应该使用类NSAttributedString的值,例如代替UITextAttributeTextColor,你应该使用NSForegroundColorAttributeName (2认同)

Kam*_*pai 41

回答iOS 8.0和Swift.

Objectice C代码:

// 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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)


Sha*_*ssy 7

这是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];
Run Code Online (Sandbox Code Playgroud)