更改UIBarButtonItem文本字体大小和颜色

时间:2015-08-11 13:56:07

标签: ios swift uibarbuttonitem

我想知道如何更改BarButtonItem的文字字体?虽然我无法设置setTitleTextAttributes

class TextfieldLogin : UITextField {

func INIT(){
    let numberToolbar = UIToolbar(frame: CGRectMake(0,0,320,50))
    numberToolbar.tintColor = UIColor.whiteColor()
    numberToolbar.barTintColor = UIColor(red: 155.0/255, green: 14.0/255, blue: 45.0/255, alpha: 1.0)

    numberToolbar.items = [
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
        UIBarButtonItem(title: "DONE", style: UIBarButtonItemStyle.Plain, target: self, action: "close"),
        UIBarButtonItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(18.0),NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
            forState: UIControlState.Normal)        ]

    numberToolbar.sizeToFit()

    }
}

2 个答案:

答案 0 :(得分:12)

这是你想要的吗?

目标-C:

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

夫特:

let titleDict: NSDictionary = [NSFontAttributeName: "Helvetica-Bold"]
self.numberToolbar.titleTextAttributes = titleDict

答案 1 :(得分:3)

与所有UIBarItem一样,您可以使用以下方式设置文字属性:

- (void)setTitleTextAttributes:(NSDictionary *)attributes
                      forState:(UIControlState)state

您还可以使用tintColor更改文字颜色。

我建议坚持使用Apple的样式指南,只在done项目上使用粗体字。