2013-07-02 28 views
2

我的應用程序中有許多UILabel元素,我正在尋找一種使用UIAppearance代理對象設計風格的簡單方法。我目前的做法是創建UILabel的不同子類,並配備UI_APPEARANCE_SELECTOR裝飾器,以便通過[UILabelSubclass appearance]調用訪問。你可以找到我使用的來源herehere。 問題是我不想設置字體大小,而只是依賴於故事板中定義大小的字體系列。使用UIAppearance代理設計UILabel

如何在子類中設置它?

回答

1

我認爲你可以在你的子類是這樣嘗試:

UIFont *newFont = [UIFont fontWithName:@"YourFontName" size:self.font.pointSize]; 
self.font = newFont 
7

這裏有一個簡單的類別,與UIAppearance代理工作:

@interface UILabel (FontName) 

- (void)setFontName:(NSString *)name UI_APPEARANCE_SELECTOR; 

@end 


@implementation UILabel (FontName) 

- (void)setFontName:(NSString *)name { 
    self.font = [UIFont fontWithName:name size:self.font.pointSize]; 
} 

@end 
+0

這也是一個很好的解決方案。我標記了另一個,只是因爲與我正在執行的內容有點接近。 – Claus

+0

這不起作用 – JAHelia

+0

我可以證實這對我有用。謝謝! – Emilio

1

多虧了這個帖子,也從一個博客帖子Peter Steinberger(http://petersteinberger.com/)我能夠找到適合自己的東西:

@interface UILabel (FontAppearance) 
@property (nonatomic, copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR; 
@end 


@implementation UILabel (FontAppearance) 
- (void)setAppearanceFont:(UIFont*)font 
{ 
    [self setFont:font]; 
} 

-(UIFont*)appearanceFont 
{ 
    return [self font]; 
} 
@end 
相關問題