UILabel+Extension.swift 3.76 KB
//
//  UILabel +.swift
//  Twear
//
//  Created by yangbin on 2021/12/15.
//

import UIKit
import SwiftDate

extension UILabel {
    
    
    func toTimeType1(length: Int, _ timeFont: UIFont = BoldFont(24), _ unitFont: UIFont = RegularFont(11)) {
        var timeStr = NSMutableAttributedString(string: "")
        let hStr = LocString("小时")
        let minStr = LocString("分钟")
        if length < 0 {
            timeStr = NSMutableAttributedString(string: "-- \(hStr) -- \(minStr)")
        } else {
            let hour = String(format:"%02d",length/60)
            let minute = String(format:"%02d",length%60)
            timeStr = NSMutableAttributedString(string: "\(hour) \(hStr) \(minute) \(minStr)")
        }
        timeStr.addAttributes([.font: timeFont], range: NSMakeRange(0, 2))
        timeStr.addAttributes([.font: unitFont], range: NSMakeRange(2, hStr.count+2))
        timeStr.addAttributes([.font: timeFont], range: NSMakeRange(hStr.count+4, 2))
        timeStr.addAttributes([.font: unitFont], range: NSMakeRange(hStr.count+6, minStr.count+1))
        self.attributedText = timeStr
    }
    
    
    func toTimeType2(length: Int, _ timeFontSize: CGFloat = 16, _ unitFontSize: CGFloat = 11) {
        var timeStr = NSMutableAttributedString(string: "")
        if length < 0 {
            timeStr = NSMutableAttributedString(string: "-- h -- m")
        } else {
            let hour = String(format:"%02d",length/60)
            let minute = String(format:"%02d",length%60)
            timeStr = NSMutableAttributedString(string: "\(hour) h \(minute) m")
        }
        timeStr.addAttributes([.font: BoldFont(timeFontSize)], range: NSMakeRange(0, 2))
        timeStr.addAttributes([.font: RegularFont(unitFontSize)], range: NSMakeRange(2, 3))
        timeStr.addAttributes([.font: BoldFont(timeFontSize)], range: NSMakeRange(5, 2))
        timeStr.addAttributes([.font: RegularFont(unitFontSize)], range: NSMakeRange(7, 2))
        self.attributedText = timeStr
    }
    func toTimeType3(length: Int) {

            let hour = String(format:"%02d",length/3600)
            let minute = String(format:"%02d",length/60)
            let second = String(format:"%02d",length%60)
        self.text = "\(hour):\(minute):\(second)"
    }


    func toTimePeriod(startDate: Date, length: Int) {
        let endDate = startDate.date + length.minutes
        let str =  "\(String(format:"%02d",startDate.hour)):\(String(format:"%02d",startDate.minute))-\(String(format:"%02d",endDate.hour)):\(String(format:"%02d",endDate.minute))"
        self.text = str
    }
    
    func toTimePeriodBySecond(startDate: Date, length: Int) {
        let endDate = startDate.date + length.seconds
        let str =  "\(String(format:"%02d",startDate.hour)):\(String(format:"%02d",startDate.minute))-\(String(format:"%02d",endDate.hour)):\(String(format:"%02d",endDate.minute))"
        self.text = str
    }
    
    func toUnitMode(text: String, unit: String, font: UIFont, unitFont: UIFont, unitColor: UIColor = .black) {
        let timeStr = NSMutableAttributedString(string: "\(text)\(unit)")

        timeStr.addAttributes([.font: font], range: NSMakeRange(0, text.count))
        timeStr.addAttributes([.font: unitFont, .foregroundColor: unitColor], range: NSMakeRange(text.count, unit.count))
        self.attributedText = timeStr
    }
    
    func toUnitMode1(title: String, text: String, unit: String, font: UIFont, unitFont: UIFont) {
        let str = NSMutableAttributedString(string: "\(title)\(text)\(unit)")

        str.addAttributes([.font: unitFont], range: NSMakeRange(0, title.count))
        str.addAttributes([.font: font], range: NSMakeRange(title.count, text.count))
        str.addAttributes([.font: unitFont], range: NSMakeRange(text.count+title.count, unit.count))
        self.attributedText = str
    }
    
}