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

111 lines
3.3 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 {
@objc static let SingleLineIdentifier = "EQNCustomAnnotationViewSingleLine"
@objc static let DoubleLineIdentifier = "EQNCustomAnnotationViewDoubleLine"
private static let HeightLabel: CGFloat = 15.0
// MARK: - Public
@objc public override var image: UIImage? {
set { imageView.image = newValue }
get { imageView.image }
}
@objc public var title: String? {
set { labelTop.text = newValue }
get { labelTop.text }
}
@objc public var subtitle: String? {
set { labelBottom.text = newValue }
get { labelBottom.text }
}
// MARK: - UI
private lazy var imageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var labelTop: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 11, weight: .medium)
label.textAlignment = .center
return label
}()
private lazy var labelBottom: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 11, weight: .medium)
label.textAlignment = .center
label.textColor = .red
return label
}()
// MARK: - Init
@objc
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
backgroundColor = .clear
if reuseIdentifier == Self.SingleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 40)
setupUI()
} else if reuseIdentifier == Self.DoubleLineIdentifier {
frame = CGRect(x: 0, y: 0, width: 40, height: 55)
setupDoubleLineUI()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Private
private func setupUI() {
let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel)
labelTop.frame = labelFrame
addSubview(labelTop)
let imageViewHeight = frame.height - labelFrame.height
imageView.frame = CGRect(x: 0, y: labelFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
let yOffeset = frame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0)
centerOffset = CGPoint(x: 0, y: yOffeset)
}
private func setupDoubleLineUI() {
let labelTopFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel)
labelTop.frame = labelTopFrame
addSubview(labelTop)
let imageViewHeight = frame.height - labelTopFrame.height * 2.0
imageView.frame = CGRect(x: 0, y: labelTopFrame.height, width: frame.width, height: imageViewHeight)
addSubview(imageView)
labelBottom.frame = CGRect(x: 0, y: imageView.frame.maxY, width: frame.width, height: Self.HeightLabel)
addSubview(labelBottom)
}
}