UIButton Swift上的多行标签

Tri*_*ton 3 xcode label button multiline swift

我一直在尝试显示一个同步按钮Last Sync at (time&date)。但是我所得到的只是一行缩短的文字。

// Sync Button

syncBtn.frame = CGRectMake(15, height-60, 120, 40)

syncBtn.addTarget(self, action: "syncBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)

syncBtn.setTitle("Last Sync: Never", forState: UIControlState.Normal)

syncBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

syncBtn.layer.borderColor = UIColor.whiteColor().CGColor

syncBtn.layer.borderWidth = 1

syncBtn.layer.cornerRadius = 5

syncBtn.titleLabel?.font = UIFont(name: "Avenir", size: 10)

syncBtn.alpha = 0.5

syncBtn.titleLabel?.sizeToFit()

syncBtn.titleLabel?.textAlignment = NSTextAlignment.Center

syncBtn.titleLabel?.numberOfLines = 2

self.view.addSubview(syncBtn)
Run Code Online (Sandbox Code Playgroud)

这是获取和设置标签上日期的功能

func printTimestamp(){
    var timestamp = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .ShortStyle, timeStyle: .ShortStyle)
    self.syncBtn.titleLabel?.text = ("Last Sync at: " + timestamp)
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解决这个问题?

ViJ*_*had 5

创建具有多行文本的UIButton

以编程方式可能

  override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
      btnTwoLine.setTitle("Mulitple line", forState:     UIControlState.Normal)
      btnTwoLine.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping;

    }
Run Code Online (Sandbox Code Playgroud)