Blame view

HDFwear/Tools/UILabel+Extension.swift 4.32 KB
f2cf74c7   yangbin   1.0.20(4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  //
  //  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%3600/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: unitFont], range: NSMakeRange(0, timeStr.length))
          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, str.length-1))
  //        str.addAttributes([.font: unitFont], range: NSMakeRange(0, title.count))
          
  //        str.addAttributes([.font: unitFont], range: NSMakeRange(text.count+title.count, unit.count))
          if AppSettings.shared.language == .Thai {
              if title == LocString("目标步数") || title == LocString("热量") {
                  str.addAttributes([.font: font], range: NSMakeRange(title.count+1, text.count))
              } else {
                  str.addAttributes([.font: font], range: NSMakeRange(title.count, text.count))
              }
          } else {
              str.addAttributes([.font: font], range: NSMakeRange(title.count, text.count))
          }
          self.attributedText = str
      }
      
  }