我已经实现了使用此solution为我的标注设置自定义字体和颜色的功能,但是它产生了一种奇怪的动画,因为它首先根据先前的字体设置了大小,然后使用新字体调整了框的大小。检查一下:
更改字体和颜色的代码
@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)
}
}
}
}