How do you set the font size on a UIBarButtonItem?

前端 未结 5 1941
旧巷少年郎
旧巷少年郎 2021-02-03 19:42

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

5条回答
  •  余生分开走
    2021-02-03 19:43

    In a easy way, simply:

    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;
    

提交回复
热议问题