MKAnnotationView标注的自定义字体 [英] Custom font for MKAnnotationView Callout

查看:158
本文介绍了MKAnnotationView标注的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

幸运的是,MKAnnotationView的标准标注视图可以满足我们的需求-titlesubtitleleftCalloutAccessoryViewrightCalloutAccessoryView.

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没有提供实现此目的的标准方法.

MKAnnotationView provides no standard way to accomplish this.

如何在MKAnnotation的标注视图中使用自定义字体?

How can I use a custom font in an MKAnnotation's callout view?

推荐答案

如果您需要的只是自定义字体,则需要将MKAnnotationView子类化,但是不必重新创建所有免费获得的行为使用标准MKAnnotationView.其实很简单.

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 standard MKAnnotationView. It's actually pretty easy.

  1. 子类
  2. 覆盖-layoutSubviews
  3. 选择MKAnnotationView时,标注将添加为子视图.因此,我们可以递归地遍历子类的子视图并找到要修改的UILabel.
  4. 就是这样!
  1. Subclass MKAnnotationView
  2. Override -layoutSubviews
  3. When an MKAnnotationView is selected, the callout is added as a subview. Therefore, we can recursively loop through our subclass' subviews and find the UILabel we wish to modify.
  4. That's it!

此方法的唯一缺点是,如果您的字体小于或大于预期的标准系统字体,则可以看到标注调整其大小.如果所有调整都在呈现给用户之前进行,那就太好了.

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.

// elsewhere, in a category on UIView.
// thanks to this answer: http://stackoverflow.com/a/25877372/607876
//
typedef void(^ViewBlock)(UIView *view, BOOL *stop);

@interface UIView (Helpers)
- (void)loopViewHierarchy:(ViewBlock)block;
@end

@implementation UIView (Helpers)

- (void)loopViewHierarchy:(ViewBlock)block {
    BOOL stop = false;

    if (block) {
        block(self, &stop);
    }

    if (!stop) {
        for (UIView* subview in self.subviews) {
            [subview loopViewHierarchy:block];
        }
    }
}

@end

// then, in your MKAnnotationView subclass
//
@implementation CustomFontAnnotationView
- (void)layoutSubviews
{
    // MKAnnotationViews only have subviews if they've been selected.
    // short-circuit if there's nothing to loop over
    if (!self.selected) {
        return;
    }

    [self loopViewHierarchy:^(UIView *view, BOOL *stop) {
        if ([view isKindOfClass:[UILabel class]]) {
            *stop = true;
            ((UILabel *)view).font = {custom_font_name};
        }
    }];
}
@end

我通过首先创建我的MKAnnotationView子类并在覆盖的-layoutSubviews中设置一个断点来检查视图树.然后,在调试器中,发布po [self recursiveDescription].确保在首次加载地图时关闭断点,因为如上所述,MKAnnotationView在选择子视图之前没有任何子视图.在做出选择之前,请启用断点,点一下图钉,断开,然后打印出视图树.您会在树的最底部看到UILabel.

I inspected the view tree by first creating my MKAnnotationView subclass and setting a breakpoint in my overridden -layoutSubviews. In the debugger, I then issued po [self recursiveDescription]. Make sure to turn the breakpoint off when your map first loads, because as mentioned up above, MKAnnotationViews 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 a UILabel at the very bottom of the tree.

这篇关于MKAnnotationView标注的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆