Fortunately, the standard callout view for an MKAnnotationView
meets our needs - title
, subtitle
, leftCalloutAccessoryView
, and rightCalloutAccessoryView
.
Unfortunately, we use custom fonts in our app, and would like to extend those custom fonts to these callouts.
MKAnnotationView provides no standard way to accomplish this.
How can I use a custom font in an MKAnnotation
's callout view?
Since I needed the Swift version - here it is. Also, you have to call setNeedsLayout() on didAddSubview() because otherwise when you deselect and reselect the annotation layoutSubviews() is not called and the callout has its old font.
If all you need is a custom font, you need to subclass
MKAnnotationView
, but you don't have to recreate all the behavior that you get for free with a standardMKAnnotationView
. It's actually pretty easy.MKAnnotationView
MKAnnotationView
is selected, the callout is added as a subview. Therefore, we can recursively loop through our subclass' subviews and find theUILabel
we wish to modify.The only drawback with this method is that you can see the callout adjust it's size if your font is smaller or larger than the standard system font it was expecting. It'd be great if all the adjustments were made before being presented to the user.
I inspected the view tree by first creating my
MKAnnotationView
subclass and setting a breakpoint in my overridden-layoutSubviews
. In the debugger, I then issuedpo [self recursiveDescription]
. Make sure to turn the breakpoint off when your map first loads, because as mentioned up above,MKAnnotationView
s don't have any subviews until their selected. Before you make a selection, enable the breakpoint, tap your pin, break, and print out the view tree. You'll see aUILabel
at the very bottom of the tree.