Created
May 31, 2017 22:17
-
-
Save ChenCodes/7586948e22cebadaad5a4bc4b8e66eac to your computer and use it in GitHub Desktop.
scheduleModule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let events: [String] = ["7:00 AM Hit the gym with Alex", "8:30 AM Coffee with Sarah", "11:00 AM Team Meeting", "3:30 PM Budget review"] | |
// Do any additional setup after loading the view, typically from a nib. | |
let offset = 20 | |
var counter = 0 | |
for value in events { | |
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 21)) | |
label.center = CGPoint(x: 160, y: 285 + counter * offset) | |
label.textAlignment = .left | |
label.textColor = UIColor.gray | |
let myMutableString = NSMutableAttributedString( | |
string: value, | |
attributes: [:]) | |
var endPos = -1 | |
var morning = false | |
if let range = value.range(of: "AM") { | |
morning = true | |
endPos = value.distance(from: value.startIndex, to: range.upperBound) | |
} | |
if let range = value.range(of: "PM") { | |
endPos = value.distance(from: value.startIndex, to: range.upperBound) | |
} | |
if morning { | |
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange(location:0, length: endPos)) | |
} else { | |
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: NSRange(location:0, length: endPos)) | |
} | |
label.attributedText = myMutableString | |
counter += 1 | |
self.view.addSubview(label) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment