【问题标题】:iPhone SDK 3.2 and UIAppFontsiPhone SDK 3.2 和 UIAppFonts
【发布时间】:2011-02-17 15:30:02
【问题描述】:

我已将我的自定义字体添加到 UIAppFonts 并且加载得很好:(显示在 [UIFont familyNames] 中)。当我在viewDidLoad { [myLabel setFont: [UIFont fontWithName:@"CustomFont" size: 65.0]]; } 中手动设置字体时,一切正常并呈现字体。

但是在 IB 中做同样的事情不会(使用其他一些默认字体代替)。必须为每个标签创建 IBOutlets 并在 viewDidLoad 中手动修复字体非常痛苦。

其他人在获取自定义字体支持以使用 3.2 SDK 和 IB 时遇到问题?

【问题讨论】:

    标签: iphone iphone-sdk-3.2 uifont


    【解决方案1】:

    向 Apple 打开了一份错误报告,结果发现它确实是一个错误。我最终使用的解决方法是:

    // LabelQuake.h
    @interface LabelQuake : UILabel
    @end
    
    // LabelQuake.m
    @implementation LabelQuake
    
        - (id)initWithCoder:(NSCoder *)decoder {
            if (self = [super initWithCoder: decoder]) {
                [self setFont: [UIFont fontWithName: @"Quake" size: self.font.pointSize]];
            }
    
            return self;
        }
    @end
    

    在我们的blog写了更长的帖子。

    【讨论】:

    • 使用上面的解决方案时,它可以工作,但由于某种原因,所有自定义标签都不会在标签中垂直居中 - 而是将文本全部对齐到顶部。你注意到了吗?它的效果是抵消了我所有的标签,让它看起来格格不入。
    • 针对我自己上面的评论,我通过在设置新字体之前检查 self.font.ascender 来“解决”这个问题,然后将整个标签偏移 after i> 你设置字体。
    【解决方案2】:

    有类似的问题。并以这种方式修复它...

    将我的自定义字体添加到我的资源组。然后通过下面给出的代码加载所有字体:

    - (NSUInteger) loadFonts{
    NSUInteger newFontCount = 0;
    NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
    const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
    if (frameworkPath) {
        void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
        if (graphicsServices) {
            BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
            if (GSFontAddFromFile)
                for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
                    newFontCount += GSFontAddFromFile([fontFile UTF8String]);
        }
    }
    
    return newFontCount;}
    
    
     - (id)initWithCoder:(NSCoder *)decoder {
        //load the fonts
        [self loadFonts];
    
        if (self = [super initWithCoder: decoder]) {
            [self setFont: [UIFont fontWithName: @"Quake" size: self.font.pointSize]];
        }
    
        return self;
    }
    

    希望它会奏效。

    【讨论】:

    • 没有将字体添加到 Info.plist 中的 UIAppFonts 属性导致它们被加载?
    • 你在这里的这个小技巧让我很高兴 :) 有了这个,我可以在针对 iOS
    • 这些值是多少 RTLD_NOLOAD | RTLD_LAZY??
    • @prajakta...抱歉回复晚了...最好看看apple reference
    【解决方案3】:

    如果您不想进行子类化,这个解决方案对我来说既快速又肮脏。当然,它假设所有标签都具有相同的字体,在我的例子中就是这样。

    for (UIView *v in view.subviews) {
    
        if ([v isKindOfClass:[UILabel class]]) {
          UILabel *label = (UILabel*)v;
    
          [label setFont:[UIFont fontWithName:@"Quake" size:label.font.pointSize]];
        }
    }
    

    我把它放在一个辅助类中,然后调用它,传入我当前的视图。

    【讨论】:

    • 而不是更改每种字体。将 UIView.tag 设置为您要更改的标签,然后在此循环中检查它也可以工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    相关资源
    最近更新 更多