2010-05-10 80 views
8

我已將我的自定義字體添加到UIAppFonts,並且加載得很好:(出現在[UIFont familyNames]中)。當我手動設置字體在viewDidLoad { [myLabel setFont: [UIFont fontWithName:@"CustomFont" size: 65.0]]; }一切正常,並呈現字體。iPhone SDK 3.2和UIAppFonts

但是在IB中做同樣的事情不會(使用其他一些默認字體)。必須爲每個標籤創建IBOutlets並在viewDidLoad中手動修復字體是非常痛苦的。

其他人有問題獲得自定義字體支持使用3.2 SDK和IB?

回答

2

用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寫多一點的時間後。

+0

當使用上面的解決方案時,它可以工作,但由於某些原因,所有自定義標籤不會將文本垂直居中放置在標籤中,而是將文本捕捉到頂部。你注意到了嗎?它具有抵消所有我的標籤並使其全部看起來不符合的效果。 – Andrew 2011-01-05 21:57:49

+1

爲了迴應上面我自己的評論,我在設置新字體之前通過檢查self.font.ascender來「解決」了問題,然後通過設置字體的差異_after_偏移整個標籤。 – Andrew 2011-01-05 22:13:07

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

希望它能工作。

+1

沒有將您的字體添加到Info.plist中的UIAppFonts屬性導致它們被加載? – Tarmo 2010-06-08 10:38:10

+0

可能... 但它適用於我...... – makboney 2010-06-08 14:55:01

+0

這個小小的黑客你在這裏讓我很開心:)有了這個,我可以輕鬆地使用自定義字體,當目標iOS <3.2時。它使用私人框架嗎? – 2010-09-13 12:47:07

1

如果你不想要繼承子類,這個解決方案對我來說很快而且很髒。當然,它假設所有的標籤都具有相同的字體,在我的情況下是這樣。

for (UIView *v in view.subviews) { 

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

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

我把這個放在一個輔助類中,只是調用它,傳遞我當前的視圖。

+0

而不是更改每個字體。將UIView.tag設置爲你想改變的標籤,然後在這個循環中檢查它也是可行的 – 2012-02-21 13:18:03