我想从标签中获取时间09.30,因为hr = 09,min = 30,可能吗?

时间:2019-04-10 08:58:02

标签: ios swift uilabel

从标签09.30获取时间,如果标签时间为08.12,我想将其作为hr = 09和min = 30,对应的值如hr = 08&min = 12,这可能吗?

2 个答案:

答案 0 :(得分:2)

要获取hourminutes

let title = timeLbl.text?.split(separator: ".")

if let hourString = title?.first, let hour = Int(hourString) {
    print(hourString)
    print(hour)
}

if let minutesString = title?.last, let minutes = Int(minutesString) {
    print(minutesString)
    print(minutes)
}

要为title设置两行label

let titleString = timeLbl.text?.replacingOccurrences(of: ".", with: "\n")
timeLbl?.numberOfLines = 0
timeLbl?.text = titleString

答案 1 :(得分:1)

let splitedStr = timeLbl.text?.split(separator: ".")
if let minStr = splitedStr?.first, let min = Int(minStr),
    let secStr = splitedStr?.last, let sec = Int(secStr) {
    var totalSec = (min * 60) + sec
    totalSec += 1
    timeLbl.text = String(format: "%02d.%02d", (totalSec/60),(totalSec%60)  )
}