Change the font of a UIBarButtonItem

前端 未结 16 1026
春和景丽
春和景丽 2020-12-07 09:18

\"UIToolbar

I have a UIBarButtonItem in my UIToolbar titled

相关标签:
16条回答
  • 2020-12-07 09:52

    These are great answers above. Just updating for iOS7:

    NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
        [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-12-07 09:52

    swift 3

    barButtonName.setTitleTextAttributes( [NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal) 
    
    0 讨论(0)
  • 2020-12-07 09:53

    To be precise, this can be done as below

    [buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
        [UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
        [UIColor greenColor], NSForegroundColorAttributeName,
        nil] 
                              forState:UIControlStateNormal];
    

    Or with object literal syntax:

    [buttonItem setTitleTextAttributes:@{
         NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
         NSForegroundColorAttributeName: [UIColor greenColor]
    } forState:UIControlStateNormal];
    

    For convenience, here's the Swift implementation:

    buttonItem.setTitleTextAttributes([
            NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
            NSAttributedStringKey.foregroundColor: UIColor.green],
        for: .normal)
    
    0 讨论(0)
  • 2020-12-07 09:53

    You can create a custom UIView programmatically:

    UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
    

    Then add images, labels or whatever you like to your custom view:

    [buttonItemView addSubview:customImage];
    
    [buttonItemView addSubview:customLabel];
    
    ...
    

    Now put it in your UIBarButtomItem.

    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
    

    And finally add barButtonItem to you navigation bar.

    0 讨论(0)
  • 2020-12-07 09:54

    Swift 5 Implementation

    rightButtonItem.setTitleTextAttributes([
                NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
                NSAttributedString.Key.foregroundColor: UIColor.green],
            for: .normal)
    
    0 讨论(0)
  • 2020-12-07 09:54

    Throughout App:

    if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
            UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
    
        }
    
    0 讨论(0)
提交回复
热议问题