【问题标题】:Change UIBarButtonItem Text Font Size and Color更改 UIBarButtonItem 文本字体大小和颜色
【发布时间】:2015-08-11 13:56:07
【问题描述】:

我想知道如何更改 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()

    }
}

【问题讨论】:

    标签: ios swift uibarbuttonitem


    【解决方案1】:

    这是你要找的吗?

    目标-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
    

    【讨论】:

    • 你能自定义你对 swift 的答案吗,提问者需要 swift 中的答案
    • 谢谢@Anbu.Karthik。是的,我需要 Swift 的答案。
    • 更新了我的答案以包含 Swift,如果您需要稍微更改一下,我不太会使用 Swift。你明白设置那些 NavBar TitleTextAttributes 的概念对吗?
    • @MSU_Bulldog 我无法在 UITextField 类中调用 navigationController。请检查
    • 更新我的答案:怎么样,对你有用吗?
    【解决方案2】:

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

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

    您也可以使用tintColor 更改文本颜色。

    我建议遵守 Apple 的样式指南,仅在 done 项目上使用粗体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      相关资源
      最近更新 更多