iPhone SDK 3.2 和 UIAppFonts

发布于 2024-08-31 22:29:31 字数 326 浏览 10 评论 0原文

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

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

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

I've added my custom font to UIAppFonts and it's loaded just fine: (shows up in [UIFont familyNames] ). When I manually set the font in viewDidLoad { [myLabel setFont: [UIFont fontWithName:@"CustomFont" size: 65.0]]; } everything works and the font is rendered.

However doing the same thing in IB doesn't (some other default font is used instead). Having to create IBOutlets for each label and fixing up the fonts manually in viewDidLoad is pretty painful.

Anyone else had problems getting the custom font support to work with 3.2 SDK and IB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨落□心尘 2024-09-07 22:29:31

向 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

在我们的 博客

Opened a bug report with Apple and turns out it really is a bug. The workaround I ended up using is this:

// 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

Wrote a bit longer post at our blog.

淡写薰衣草的香 2024-09-07 22:29:31

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

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

- (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;
}

希望它能工作。

had simillar kind of problem.and fixed it in this way...

add my custom font to my Resource group. then load all the fonts by the code given bellow:

- (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;
}

Hope it will work.

紫竹語嫣☆ 2024-09-07 22:29:31

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

for (UIView *v in view.subviews) {

    if ([v isKindOfClass:[UILabel class]]) {
      UILabel *label = (UILabel*)v;

      [label setFont:[UIFont fontWithName:@"Quake" size:label.font.pointSize]];
    }
}

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

If you don't want to have to subclass, this solution worked quick and dirty for me. Of course it assumes that all labels have the same font, and in my case that was the case.

for (UIView *v in view.subviews) {

    if ([v isKindOfClass:[UILabel class]]) {
      UILabel *label = (UILabel*)v;

      [label setFont:[UIFont fontWithName:@"Quake" size:label.font.pointSize]];
    }
}

I put this in a helper class and just called it, passing in my current view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文