如何更改按钮的文字大小?

时间:2016-02-24 12:25:20

标签: swift uibutton

我需要更改文字的大小。我该怎么做?这是我的代码:

  let playAgain: UIButton = UIButton(frame: CGRectMake(140, 400, 100, 50))
  playAgain.setTitle("Play Again", forState: UIControlState.Normal)


  playAgain.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
  playAgain.tag = 1
  self.view!.addSubview(playAgain)

3 个答案:

答案 0 :(得分:1)

设置按钮的titleLabel.font属性。

playAgain.titleLabel!.font =  UIFont(name: "Helvetica", size: 20)

答案 1 :(得分:1)

如果您想在保持字体原样的同时只改变字体的大小,可以使用:

playAgain.titleLabel.font = UIFont(name: playAgain.titleLabel.font.fontName, size: /*whatever size you like*/ )

答案 2 :(得分:0)

要更改按钮的字体,您必须访问titleLabel属性。

playAgain.titleLabel!.font = //... the font you want to use

UIButton的文档中对此进行了解释。

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/