开发者

How do you set the font size on a UIBarButtonItem?

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 sug开发者_JAVA技巧gestions?


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;


Create a UILabel and use -initWithCustomView:.


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.)


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


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)


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜