How do you use setTitleTextAttributes:forState in

2019-01-16 06:30发布

How do you use setTitleTextAttributes:forState: in UIBarItem in iOS?

How do you set the NSDictionary? Can't make it work and documentation isn't very clear about that.

From the documentation:

setTitleTextAttributes:forState:

Sets the title’s text attributes for a given control state:

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

Parameters:

attributes: A dictionary containing key-value pairs for text attributes. You can specify the font, text color, text shadow color, and text shadow offset using the keys listed in NSString UIKit Additions Reference.

state: The control state for which you want to set the text attributes for the title.

4条回答
再贱就再见
2楼-- · 2019-01-16 07:10

Answer for iOS 8.0 and Swift.

Objectice C code:

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

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)
查看更多
时光不老,我们不散
3楼-- · 2019-01-16 07:22

Example code:

    [[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];
查看更多
Explosion°爆炸
4楼-- · 2019-01-16 07:22

Here's phix23's code, just with an updated, and I think cleaner, syntax:

[[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];
查看更多
太酷不给撩
5楼-- · 2019-01-16 07:29
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIColor whiteColor], nil] forKeys:[NSArray arrayWithObjects:UITextAttributeTextColor, nil]] forState:UIControlStateNormal];
查看更多
登录 后发表回答