【问题标题】:How do you use setTitleTextAttributes:forState in UIBarItem?你如何在 UIBarItem 中使用 setTitleTextAttributes:forState?
【发布时间】:2011-12-10 06:24:39
【问题描述】:

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

如何设置NSDictionary?无法使其工作,文档对此也不是很清楚。

来自文档:

setTitleTextAttributes:forState:

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

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

参数:

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

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

【问题讨论】:

    标签: ios objective-c swift uinavigationcontroller uibaritem


    【解决方案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];
    

    【讨论】:

    • Swift 4 实际上如下:[NSAttributedStringKey.foregroundColor: color] 你应该更新你的代码。
    【解决方案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];
      

      【讨论】:

        【解决方案5】:

        Swift5设置UIBarItem标题颜色

        let attributes = [
            NSAttributedString.Key.foregroundColor : UIColor.orange,
            NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20)
        ]
        vc.tabBarItem.setTitleTextAttributes(attributes, for: .normal)
        

        【讨论】:

          猜你喜欢
          • 2017-01-21
          • 2021-08-03
          • 2021-07-09
          • 2010-12-06
          • 2015-10-31
          • 2019-11-17
          • 2010-10-27
          • 2012-02-16
          • 2017-03-09
          相关资源
          最近更新 更多