3

I've been trying to get a sync button to display Last Sync at (time&date). But All I get is one line with shortened text.

// 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) 

This is the function to get and set the date on the label

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

Anyone able to solve this problem?

2 Answers 2

6

create a UIButton with Multiple lines of text

Possible programtically

 override func viewDidAppear(animated: Bool){ super.viewDidAppear(animated) btnTwoLine.setTitle("Mulitple line", forState: UIControlState.Normal) btnTwoLine.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping; } 
Sign up to request clarification or add additional context in comments.

1 Comment

plz look here for my question stackoverflow.com/questions/46723070/…
1

Use SetTitle method instead of .text , when you use .text it sets the title text but the constraints are not updated accordingly hence you need to use SetTitle method and set the adjustsFontSizeToFitWidth to true

self.syncBtn.setTitle(timeStamp, forState: UIControlState.Normal) self.syncBtn.titleLabel?.adjustsFontSizeToFitWidth = true 

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.