【问题标题】:iOS 7 UIBarButtonItem font changes when tappediOS 7 UIBarButtonItem 字体在点击时发生变化
【发布时间】:2014-03-02 17:48:31
【问题描述】:

我正在尝试更改我的UIBarButtonItem 字体。 ViewControllers 加载时看起来不错。但是,如果我点击条形按钮,或者向右滑动就像移动到上一个 ViewController(但随后拉回当前的),字体会变回系统字体。这是我在 AppDelegate 中设置的内容:

NSDictionary* barButtonItemAttributes = @{NSFontAttributeName: [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f]};
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateHighlighted];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateSelected];
[[UIBarButtonItem appearance] setTitleTextAttributes: barButtonItemAttributes forState:UIControlStateDisabled];

这是我 viewWillAppear 的一个例子:

- (void) viewWillAppear:(BOOL)animated {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonPressed)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
}

我是以某种方式将字体改回来,还是我滥用了外观代理?

【问题讨论】:

    标签: ios objective-c fonts uibarbuttonitem


    【解决方案1】:

    问题是tintColor 设置与标题文本属性冲突。注释掉那行,一切都会好起来的。

    如果您要使用标题文本属性,则将文本颜色合并到这些属性中:

    NSDictionary* barButtonItemAttributes =
    @{NSFontAttributeName:
          [UIFont fontWithName:@"Georgia" size:20.0f],
      NSForegroundColorAttributeName:
          [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0]  
     };
    

    【讨论】:

    • 谢谢。这似乎是真的。但是如何根据每个 ViewController 更改按钮颜色?
    • 在 iOS 7 中,NSAttributedString 是一个一流的对象,因此颜色现在是一个文本属性。因此,不要使用外观代理并使其与单个 tintColor 冲突,只需制作按钮并为其文本提供所需的属性,包括字体和颜色。
    • 现在,说了这么多,事实是你在这里遇到了一个巨大的 iOS 7 错误。稍微搜索一下堆栈溢出就会发现 许多 人遇到类似问题,例如这里:stackoverflow.com/questions/18935574/… 基本上,您应该尝试将错误缩小到一个简单可证明的情况,然后将其报告给 Apple。
    • 感谢您的反馈。我想知道是否可以在 ViewController 中手动查询外观代理?那么,当我“覆盖”titleTextAttributes 时,我可以使用 AppDelegate 中已经存储的内容吗?
    • 如果问题是您不想为每个条形按钮项显式设置文本属性,但不同的条形按钮项具有不同的颜色,为什么不使用“包含在”版本的外观代理为不同的视图控制器预先设置不同的东西?
    【解决方案2】:

    通过在 viewDidLoad 方法中使用这种方法,您可以一次设置自定义文本、字体、大小等。

    UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    
    UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    buttonDone.frame = CGRectMake(10, 0, 50, 30);
    
    buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
    buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f];
    [buttonDone setTitle:@"Done" forState:UIControlStateNormal];
    [customBarButtonView addSubview:buttonDone];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
    

    【讨论】:

      【解决方案3】:
      _myrButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"French KeyBoard" style:UIBarButtonItemStylePlain target:self action:@selector(RbuttonItemdown:)];
      
      NSDictionary* itemTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"Georgia" size:12.0f], NSForegroundColorAttributeName:[UIColor blueColor], NSBackgroundColorAttributeName:[UIColor lightGrayColor]};
      
      [_myrButtonItem setTitleTextAttributes:itemTextAttributes forState:UIControlStateNormal];
      
      [self.navigationItem setRightBarButtonItem:_myrButtonItem];
      

      你可以使用所有这些:

      NSString *const  NSFontAttributeName ;
      NSString *const  NSParagraphStyleAttributeName ;
      NSString *const  NSForegroundColorAttributeName ;
      NSString *const  NSBackgroundColorAttributeName ;
      NSString *const  NSLigatureAttributeName ;
      NSString *const  NSKernAttributeName ;
      NSString *const  NSStrikethroughStyleAttributeName ;
      NSString *const  NSUnderlineStyleAttributeName ;
      NSString *const  NSStrokeColorAttributeName ;
      NSString *const  NSStrokeWidthAttributeName ;
      NSString *const  NSShadowAttributeName ;
      NSString *const  NSTextEffectAttributeName ;
      NSString *const  NSAttachmentAttributeName ;
      NSString *const  NSLinkAttributeName ;
      NSString *const  NSBaselineOffsetAttributeName ;
      NSString *const  NSUnderlineColorAttributeName ;
      NSString *const  NSStrikethroughColorAttributeName ;
      NSString *const  NSObliquenessAttributeName ;
      NSString *const  NSExpansionAttributeName ;
      NSString *const  NSWritingDirectionAttributeName ;
      NSString *const  NSVerticalGlyphFormAttributeName;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 1970-01-01
        • 2020-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多