-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathDistanceLabel.swift
52 lines (46 loc) · 1.3 KB
/
DistanceLabel.swift
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
//
// DistanceLabel.swift
// OpenGpxTracker
//
// Created by merlos on 01/10/15.
//
import Foundation
import UIKit
import MapKit
///
/// A label to display distances.
///
/// The text is displated in meters if is less than 1km (for instance "980m") and in
/// km with two decimals if it is larger than 1km (for instance "1.20km").
///
/// If `useImperial` is true, it displays the distance always in miles ("0.23mi").
///
/// To update the text displayed set the `distance` property.
///
open class DistanceLabel: UILabel {
/// Internal variable that keeps the actual distance
private var _distance = 0.0
/// Internal variable to keep the use of imperial units
private var _useImperial = false
/// Use imperial units (miles)? False by default.
/// If true, displays meters and kilometers
open var useImperial: Bool {
get {
return _useImperial
}
set {
_useImperial = newValue
distance = _distance // Updates text displayed to reflect the new units
}
}
/// Distance in meters
open var distance: CLLocationDistance {
get {
return _distance
}
set {
_distance = newValue
text = newValue.toDistance(useImperial: _useImperial)
}
}
}