1

I have achieved to set a custom font and color for my callout using this solution, but it produces a strange animation because it first sets the size according to the previous font and then resizes the box with the new one check this:

Strange animation

Code to change font and color

@objc class CustomAnnotationView: MKAnnotationView {

override func didAddSubview(_ subview: UIView) {

    if isSelected {
        setNeedsLayout()
    }
}

override func layoutSubviews() {

    if !isSelected {
        return
    }

    loopViewHierarchy { (view: UIView) -> Bool in
        if let label = view as? UILabel {
            label.font = ViewUtil.fontMediumWithSize(14)
            label.textColor = ViewUtil.BlueGray
            return false
        }
        return true
    }

    super.layoutSubviews()
}
}

typealias ViewBlock = (_ view: UIView) -> Bool

extension UIView {

func loopViewHierarchy(block: ViewBlock?) {

    if block?(self) ?? true {
        for subview in subviews {
            subview.loopViewHierarchy(block: block)
        }
    }
}
}
aleixrr
  • 461
  • 4
  • 18

0 Answers0