Change the font of a UIBarButtonItem

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

\"UIToolbar

I have a UIBarButtonItem in my UIToolbar titled

16条回答
  •  天命终不由人
    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)
    

提交回复
热议问题