(1)Mode
1.1 DateAndTime
myDatePick.datePickerMode = UIDatePickerMode.DateAndTime
1.2 Date
myDatePick.datePickerMode = UIDatePickerMode.Date
1.3 Time
myDatePick.datePickerMode = UIDatePickerMode.Time
1.4 CountDownTimer
myDatePick.datePickerMode = UIDatePickerMode.CountDownTimer
(2)Locale(預設為英文)
將Locale設為繁體中文
myDatePick.locale=NSLocale(localeIdentifier: "ZH_TW")
日文
myDatePick.locale=NSLocale(localeIdentifier: "ja")
設定DatePick的日期時間
將時間設為目前日期程式碼如下
(4)MinimumDate
最小選取日期,例如我們只希望客戶最早只能選取到昨天的日期
myDatePick.datePickerMode = UIDatePickerMode.DateAndTime
myDatePick.date = NSDate()
// 設置 NSDate 的格式
let formatter = NSDateFormatter()
// 設置時間顯示的格式
formatter.dateFormat = "yyyy-MM-dd HH:mm"
//最早可選擇的日期時間
let fromDateTime = formatter.dateFromString("2016-12-18 12:00")
//設定minimDate
myDatePick.minimumDate = fromDateTime
(5)日期時間顯示格式
Swift以 NSDateFormatter類別來設定日期及時間的顯示格式。
也可以指定格式後再以NSDateFormatter 方法將日期型別轉換為字串顯示
NSDateFormatter設定格式有兩種方式,第一種是使用dateStyle以及timeStyle分別
設定日期格式及時間格式。此種方式是用系統內建的格式,只要選取名稱即可。
5.1 FullStyle
5.2 LongStyle
5.3 MidiumSytle
5.4 ShortStyle
5.5 NoStyle
例如日期和時間都採用FullStyle
//
// ViewController.swift
// compoment
//
// Created by boywhy chen on 2016/12/19.
// Copyright © 2016年 boywhy chen. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mLabel: UILabel!
@IBOutlet weak var myDatePick: UIDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myDateTime = NSDateFormatter()
myDateTime .dateStyle = NSDateFormatterStyle.FullStyle
myDateTime.timeStyle=NSDateFormatterStyle.FullStyle
mLabel.text = myDateTime.stringFromDate(myDatePick.date)
mLabel.numberOfLines = 0; //will wrap text in new
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
第二種方式是使用六個字母來顯示年月日時分秒。
y 西元年
M 月
d 日
h 時
m 分
s 秒
一個字母代表一個數字,若字母不足,仍會顯示完整數值,若數字不足,會在數前面面補0
例如顯示11 月,設定M和設定MM 都會顯示11 ,設定MMM則會顯示011
以下是幾種設定的範例
1. MM-dd-yyyy hh:mm:ss
var myDateTime = NSDateFormatter()
myDateTime.dateFormat = "MM-dd-yyyy hh:mm:ss"
mLabel.text = myDateTime.stringFromDate(myDatePick.date)
2. y/M/d h:m:s
var myDateTime = NSDateFormatter()
myDateTime.dateFormat = "y/M/d h:m:s"
mLabel.text = myDateTime.stringFromDate(myDatePick.date)
mLabel.numberOfLines = 0; //will wrap text in new
3.西元y年M月d日hh點mm分ss秒
var myDateTime = NSDateFormatter()
myDateTime.dateFormat = "西元y年M月d日hh點mm分ss秒"
mLabel.text = myDateTime.stringFromDate(myDatePick.date)
mLabel.numberOfLines = 0; //will wrap text in new
接下來是一個小範例,當DatePick被改變時,Label也會跟著改變
//
// ViewController.swift
// compoment
//
// Created by boywhy chen on 2016/12/19.
// Copyright © 2016年 boywhy chen. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mLabel: UILabel!
@IBOutlet weak var myDatePick: UIDatePicker!
var myDateTime = NSDateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myDateTime.dateFormat = "西元y年M月d日hh點mm分ss秒"
mLabel.text = myDateTime.stringFromDate(myDatePick.date)
mLabel.numberOfLines = 0; //will wrap text in new
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func valueChange(sender: UIDatePicker) {
mLabel.text = myDateTime.stringFromDate(sender.date)
mLabel.numberOfLines = 0; //will wrap text in new
}
}
當DatePick值改變時,連結valueChange func
參考資料:
選取日期時間 UIDatePicker
https://itisjoe.gitbooks.io/swiftgo/content/uikit/uidatepicker.htmlUILabel auto resize on basis of text to be shown
http://stackoverflow.com/questions/5430890/uilabel-auto-resize-on-basis-of-text-to-be-shown
沒有留言:
張貼留言