41

I can't find a way to set the font size of the title in a custom UIBarButtonItem. The only way I can think of getting around this is to set it as an image, which I would like to avoid. Any other suggestions?

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Jim
  • 890
  • 2
  • 10
  • 22
  • Here's a similar question. http://stackoverflow.com/questions/5421121/change-font-size-via-uibarbuttonitem-in-uitextview-works-just-once – acecapades Mar 02 '12 at 06:41

6 Answers6

82

Objective-C:

NSUInteger fontSize = 20;
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
NSDictionary *attributes = @{NSFontAttributeName: font};

UIBarButtonItem *item = [[UIBarButtonItem alloc] init];

[item setTitle:@"Some Text"];
[item setTitleTextAttributes:attributes forState:UIControlStateNormal];

self.navigationItem.rightBarButtonItem = item;

Swift:

let fontSize:CGFloat = 20;
let font:UIFont = UIFont.boldSystemFont(ofSize: fontSize);
let attributes:[String : Any] = [NSFontAttributeName: font];
    
let item = UIBarButtonItem.init();
    
item.title = "Some Text";
item.setTitleTextAttributes(attributes, for: UIControlState.normal);
    
self.navigationItem.rightBarButtonItem = item;
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Mateus
  • 2,640
  • 5
  • 44
  • 62
  • 2
    You can also use the `UIAppearance` proxy to `setTitleTextAttributes`. – livingtech Jan 17 '13 at 18:17
  • 4
    Minor note: the UITextAttributeFont key value has been deprecated in iOS 7, you can use NSFontAttributeName instead. – Joe Regan Mar 14 '14 at 13:39
  • If you plan to open a popup or menu by taping the button, you should also set set text attributes for 'UIControlStateHighlighted'. – LaborEtArs Oct 08 '22 at 17:53
4

As a concrete example of what kennytm suggests, you create the UIBarButtonItem with something like the following:

UILabel *txtLabel = [[UILabel alloc] initWithFrame:rect];
txtLabel.backgroundColor = [UIColor clearColor];
txtLabel.textColor = [UIColor lightGrayColor];
txtLabel.text = @"This is a custom label";
UIBarButtonItem *btnText = [[[UIBarButtonItem alloc] initWithCustomView:txtLabel] autorelease];

Then, you can add it as centered text on a UIToolbar with the following for example:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:rect];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *flexSpace2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];

[toolBar setItems:[NSArray arrayWithItems:flexSpace1, btnText, flexSpace2, nil]];

(of course, to get proper formatting, the rect used for initializing txtLabel and toolBar should be the proper sizes.... but that's another exercise.)

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Jeff Hay
  • 2,655
  • 28
  • 32
  • 1
    You can make it the correct size using [txtLabel sizeToFit]. As a side note I'm not sure if this method will retain the blue capsule background a button item, so you may have to use some custom graphics with it. But its your best bet since you can't actually set the font size of a bar button item if you initialize it with a title. – axiixc Mar 10 '12 at 20:22
  • You should set the Frame-size of the label too. In my case no items where displayed because they initialized with width and height of 0. – Alexander Schmidt Dec 05 '13 at 10:17
3

Create a UILabel and use -initWithCustomView:.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • So do I pass the UIBarButtonItem into the UILabel and set the font normally? I might also have a problem with your method because I'm using Interface Builder to put the UIBarButtonItem into a UIToolbar. – Jim Apr 27 '10 at 15:17
  • 1
    use setTextAttributes:forState: – bigkm Jul 24 '12 at 02:59
3

Swift5:

let item = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(self.onItemTapped))
let font:UIFont = UIFont(name: "", size: 18) ?? UIFont()
item.setTitleTextAttributes([NSAttributedString.Key.font: font], for: UIControl.State.normal)
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
August Lin
  • 1,249
  • 12
  • 11
2
[[UIBarButtonItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                     [UIFont fontWithName:@"FONT-NAME" size:21.0], NSFontAttributeName, nil]
                                           forState:UIControlStateNormal];
Atef
  • 2,872
  • 1
  • 36
  • 32
1
let barButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Title",
                                                     style: .plain,
                                                     target: nil,
                                                     action: nil)
let font: UIFont = UIFont.systemFont(ofSize: 12.0)
let textAttributes: [NSAttributedString.Key: Any] = [.font: font]

barButtonItem.setTitleTextAttributes(textAttributes, for: .normal)
barButtonItem.setTitleTextAttributes(textAttributes, for: .selected)
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152