Files
eqn.ios/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift
T

71 lines
1.9 KiB
Swift

//
// EQNCustomAnnotationView.swift
// Earthquake Network
//
// Created by Andrea Busi on 06/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import Foundation
import MapKit
@objc
public class EQNCustomAnnotationView: MKAnnotationView {
static let Identifier = "EQNCustomAnnotationView"
private static let AnnotationFrame = CGRect(x: 0, y: 0, width: 40, height: 40)
public override var image: UIImage? {
set { imageView.image = newValue }
get { imageView.image }
}
public var title: String? {
set { label.text = newValue }
get { label.text }
}
// MARK: - UI
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var label: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
label.textAlignment = .center
return label
}()
// MARK: - Init
@objc
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
frame = Self.AnnotationFrame
centerOffset = CGPoint(x: 0, y: -frame.size.height / 2.0)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupUI() {
backgroundColor = .clear
let labelFrame = CGRect(x: 0, y: 0, width: Self.AnnotationFrame.width, height: 15)
label.frame = labelFrame
addSubview(label)
let imageViewHeight = Self.AnnotationFrame.height - labelFrame.height
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: Self.AnnotationFrame.width, height: imageViewHeight)
addSubview(imageView)
}
}